aws cli: How can I query list values?

Viewed 24050

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

Related