6

I have some EC2 instances. I want to use the ec2 describe-instances command to get a list of instances based on a specific value of a tag.

The table shows my use-case.

Instance  | Value (key:Purpose)     | Outcome
----------+-------------------------+------------
InstanceA | Going                   | Filter
InstanceB | Shopping,Going          | Filter
InstanceC | Going,Shoping           | Filter
InstanceD | Shopping,Going,Chatting | Filter
InstanceE | GoingGreat              | DONT Filter
InstanceF | NotGoing                | DONT Filter

So I want to somehow use wildcard in the ec2-describe-instances command so that I get the expected outcome.

Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
Deep
  • 528
  • 3
  • 12
  • 27
  • you can run `ec2-describe-instances ... |grep -v KEYWORD` to exclude the recodes from output – BMW Dec 31 '14 at 11:28
  • Thanks for the comment, however I want to use the -filters available with AWS commands and use wildcards in them. – Deep Jan 02 '15 at 06:07

1 Answers1

10

Here is an example of how to filter the output of ec2-describe-instances based on the value of a tag:

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId]' --filters "Name=platform,Values=windows" --output text

This example shows the Instance ID for all EC2 instance with the "platform" tag set to a value of "windows".

Wildcards are also permitted in the Values parameter (eg Name=platform,Values=win*).

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • Thanks a lot John, I am trying something like: aws_cmd ec2-describe-instances --region $region --filter tag:Platform=$platform_id The platform_id is a parameter coming from somewhere else...Apart from (*) wildcard I wasnt able to use any other wildcard with $platform_id. Will your syntax allow me this? I will also try it out. – Deep Jan 08 '15 at 13:48
  • You could do: Values=*$platform_id* – John Rotenstein Jan 09 '15 at 00:37
  • But this will fail in following case: I want to get only instances with Purpose = Going Now i pass this value in platform_id If i do *$platform_id* I will get all the instances from the table, whereas I just need InstanceA,B,C and D filtered because only these have 'Going' as a full string and not a part of string.Hope i was able to put my question clearly. – Deep Jan 09 '15 at 03:49
  • Yep. In that case, try to use values that don't partially overlap. Otherwise, you'll have to do some post-processing to apply more detailed logic (eg "Going" but not "NotGoing"). – John Rotenstein Jan 09 '15 at 03:55
  • Yup, thats what I have though about as a rescue. However the ideal situation would be to have the tag values whatever I like. Thanks anyway for your time and efforts. Cheers! – Deep Jan 09 '15 at 04:51