16

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?

7
  • Can't you just extract that from output?
    – Andrey
    Commented Dec 20, 2013 at 15:31
  • @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
    Commented Dec 20, 2013 at 15:35
  • but why not just do it with jq?
    – Andrey
    Commented Dec 20, 2013 at 17:38
  • Why use two tools if one will do?
    – chris
    Commented Dec 20, 2013 at 18:20
  • 2
    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
    Commented Dec 20, 2013 at 19:09

4 Answers 4

16

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
  • 1
    Hmm.. not successful, I'm getting a number of blank results instead of values [ [], [], [] ... ]. I'm on version aws-cli/1.7.3 Python/2.7.9 Windows/7
    – sonjz
    Commented Jan 21, 2015 at 21:00
  • 1
    Figured it out, it had to do with how powershell was encoding it if I was using double quotes: aws ec2 describe-instances --query "Reservations[].Instances[].Tags[?Key==InstanceName].Value[]". Notice escaping with the double-grave accents.
    – sonjz
    Commented Jan 21, 2015 at 21:10
  • 1
    If query is inside double quotes, can use single quotes inside instead of backtick: aws ec2 describe-subnets --query "Subnets[*].[AvailabilityZone,SubnetId,CidrBlock,Tags[?Key=='Name'].Value[]]"
    – David Cobb
    Commented Oct 23, 2019 at 18:11
3

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

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
0
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';

Not the answer you're looking for? Browse other questions tagged or ask your own question.