13
votes

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?

4
Can't you just extract that from output?Andrey
@Andrey: I'm hoping there's a way to have something like SecurityGroups{Name=Foo} in the query. The alternative is to use json format and pipe it to jq, but I want to make sure I'm not missing something first.chris
but why not just do it with jq?Andrey
Why use two tools if one will do?chris
I understand, but I'm trying to understand the capabilities of the native command line tools - is there a way to reference a property of a list item? You can refer to properties of structures, and select items from a list, but is it possible to combine the two?chris

4 Answers

13
votes

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[]'
3
votes

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

0
votes

This way works for me: (this only works in version 1.3.0 and above)

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId, Tags[?Key==`Name`].Value[*]]'
0
votes
select security_groups from aws.aws_ec2_instance;
> select security_groups from aws.aws_ec2_instance limit 1;
+---------------------------------------------------------------------------------------------------------------------------------+
|                                                         security_groups                                                         |
+---------------------------------------------------------------------------------------------------------------------------------+
| [{"GroupId":"sg-xxxx","GroupName":"xxxx"},{"GroupId":"sg-xxxxxx","GroupName":"xxxx"}] |
+---------------------------------------------------------------------------------------------------------------------------------+

This will just list out the security groups for your instances.

You can also use

select security_groups from aws.aws_ec2_instance where instance_id = 'i-xxxxxx';