Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws cli: How can I query list values?

The aws cli has a --query option, which allows you to select only some information.

For an example, I am interested in getting just the Security group name from ec2 describe-instances.

If I run:

aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[InstanceId,InstanceType,SecurityGroups]

my output looks like:

i-xxxxxxx m1.type [{u'GroupName': 'groupName', u'GroupId': 'sg-xxxxx'}]

I can also access elements of the list using an index:

aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[InstanceId,InstanceType,Tags[0].Value,Tags[0].Name]

Is it possible to query tags so that instead of Tag[0] I search for a Tag where the name is specified?

like image 881
chris Avatar asked Dec 20 '13 15:12

chris


2 Answers

As of 1.3.0, you can now query that information like this:

 --query 'Reservations[*].Instances[*].Tags[?Key==`<keyname>`].Value[]'

So where you have this:

      "Tags" : [
        {
          "Value" : "webserver01",
          "Key" : "InstanceName"
        },

you'd want to do this:

aws ec2 describe-instances --query 'Reservations[*].Instances[*].Tags[?Key==`InstanceName`].Value[]'
like image 166
DrStrangepork Avatar answered Oct 11 '22 00:10

DrStrangepork


What you probably want to use is the --filters option:

aws ec2 describe-instances --output text --filters "Name=tag-key, Values=SecurityGroups, Name=tag-value, Values=Foo" --region us-east-1

You can change the filters around to "query" for the exact field you're looking for.

checkout this slideshare from the Atlanta AWS meetup group's talk on the new AWS CLI for more examples

like image 26
pquery Avatar answered Oct 11 '22 00:10

pquery