aws cli: How can I query list values? - amazon-web-services

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?

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

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

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[*]]'

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

Delete EBS Snapshot From Shell Script with Name and Date FIlter

Hi I want to delete EBS snapshot which is older then 30 days and want to filter out with pharse of it's name. Let's say the name contain word 'Manish'.
The name can be like that 'Manish-Ebs-snapshot' or EBS-Manish-snapshot or EBS-ManishFinal-Snapshot' like that.
I found out one command but it's collecting all the snapshot not a particular group of snapshot.
snapshots_to_delete=$(aws ec2 describe-snapshots --owner-ids xxxxxxxxxxxx --query 'Snapshots[?StartTime<=`2020-08-23`].SnapshotId' --output text)
echo "List of snapshots to delete: $snapshots_to_delete"
for snap in $snapshots_to_delete; do
aws ec2 delete-snapshot --snapshot-id $snap
done
can anyone help me with this above script how I can add name filter as well.
The field I want:
You can filter by description using *Manish*:
aws ec2 describe-snapshots --owner-ids xxxxxxxxx --filter Name=description,Values=*Manish* --query 'Snapshots[?StartTime<=`2020-08-23`].SnapshotId'
And by tag Name:
aws ec2 describe-snapshots --owner-ids xxxxxxxx --filter Name=tag:Name,Values=*Manish* --query 'Snapshots[?StartTime<=`2020-08-23`].SnapshotId'

How do I get the value of a tag in the aws cli describe-instances output?

I want to get the instance id and the value of the name tag, this doesn't work:
aws ec2 describe-instances --filters 'Name=tag:Name,Values=InstaneNameTagPrefix*' --query 'Reservations[*].Instances[*].{InstanceId:InstanceId,Tags:Tags.Key=Name}'
After going this through JMESPath example, I was able to make it working. Try this and let me know how it goes at your end.
aws ec2 describe-instances --filters 'Name=tag:Name,Values=myDevEC2*' --query 'Reservations[*].Instances[*].{InstanceId:InstanceId,Tags:Tags[?Key == `Name`] | [0].Value}'
Output -
[
[
{
"InstanceId": "i-xxxxxxxxxxxxxx",
"Tags": "myDevEC2-123"
}
]
]
Since Tags is an array, you will need to filter to just Name pair. After that pipe the Tags result and select Value.
PS - You might want to rename JSON output second element to InstanceName instead Tags :)

How to get the InstanceID using the PublicDnsName?

With the AWS CLI, given a PublicDnsName, how can I get the InstanceID? I tried
aws ec2 describe-instances --filters 'Name=publicdnsname,Values=ec2....
but it complains that
publicdnsname is not a valid filter
It is dns-name
aws ec2 describe-instances --filters "Name=dns-name,Values=ec2-xxxxx.compute-1.amazonaws.com" --query 'Reservations[*].Instances[*].InstanceId' --output text
Output
i-00123458ca3fa2c4f
The valid filters are listed in the docs.
To filter by public DNS name the filter name you should use is: dns-name

How to remove extra parentheses and angular brackets while using aws cli?

I am trying to get the info using of NetworkInterface using following command.
[root#ip-172-29-45-82 ~]# aws ec2 describe-instances --instance-ids i-dd6f6f53 --query Reservations[*].{VpcId:Instances[*].NetworkInterfaces[*].VpcId}
[
{
"VpcId": [
[
"vpc-38fb075d"
]
]
}
]
I don't want extra parentheses and angular brackets.
It should be something like as follows:
["VpcId":"vpc-38fb075d"] or [{"VpcId":"vpc-38fb075d"}] or {"VpcId":"vpc-38fb075d"}
Is there any way to achieve above output from the above command.
Thanks in advance.
Result can be further refined by adding this to the end of your command
--output text
And the output will be unstructured text:
"VpcId": "vpc-38fb075d"
I solved it.
I used following command.
aws ec2 describe-instances --instance-ids i-dd6f6f53 --query Reservations[0].{VpcId:Instances[0].NetworkInterfaces[0].VpcId}
I just changed * to 0 and its working.
Now the output is
{
"VpcId": "vpc-38fb075d"
}
You could run - given that you can't have ENIs in different VPCs:
aws ec2 describe-instances --query 'Reservations[].Instances[].VpcId' --output text --instance-ids i-dd6f6f53
You can also try nesting your JSON objects - for example, trying to get the name tag from the parent bracket Tags:
aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId,PrivateIpAddress, [Tags[?Key==`Name`].Value][0][0]]' --output text
It is easier to develop the query using the JSON output, then cleaning things up with --output text.

How to list only the instance id and related tags

I using the CLI tools and I want to list only the instance ID and related tags of an instance. The command that I am using is ec2-describe instances. I have tried certain filters but nothing is working out.
Can anybody help ??
error :
./Instance_Audit.sh: line 24: $: command not found
./Instance_Audit.sh: line 25: syntax error near unexpected token `do'
./Instance_Audit.sh: line 25: ` do echo $ID ; echo $(aws ec2 describe-instances --query "Reservations[].Instances[?InstanceId==\`$ID\`].Tags[]") done'
Using the aws cli, I can produce a few different alternatives of the tag output:
aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[InstanceId,Tags[]]'
Result: i-xxxxx [{u'Value': 'key1value', u'Key': 'key1name'}, {u'Value': 'key2value', u'Key': 'key2name'}]
aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[InstanceId,Tags[].*]'
Result: i-xxxxx [['key1value', 'key1name'], ['key2value', 'key2name']]
aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[InstanceId,Tags[].Key,Tags[].Value]'
Result: i-xxxx ['key1name', 'key2name'] ['key1value' 'key2value']
Is any of those formats what you're looking for?
The AWS CLI command
aws ec2 describe-instances --region us-west-2 --query 'Reservations[].Instances[].[InstanceId,Tags[]]' --output text
produces these results:
i-789c55c3
Name Instance1
aws:cloudformation:logical-id Instance1
aws:cloudformation:stack-id arn:aws:cloudformation:us-west-2:012345678901:stack/test10-test10-foobarfoobar/a6e545a0-af52-11e4-a0be-50d5020578e0
aws:cloudformation:stack-name test10-test10-foobarfoobar
i-77c108cc
Name Instance2
aws:cloudformation:logical-id Instance2
aws:cloudformation:stack-id arn:aws:cloudformation:us-west-2:012345678901:stack/test10-test10-foobarfoobar/a6e545a0-af52-11e4-a0be-50d5020578e0
aws:cloudformation:stack-name test10-test10-foobarfoobar
If that is not in the format you are looking for, can you provide an example of they format you are expecting?
If you want to only display the instance id and the tags associated to that instance, you can use something like :
$ for ID in $(aws ec2 describe-instances --region eu-west-1 --query "Reservations[].Instances[].InstanceId" --output text); do echo $ID ; echo $(aws ec2 describe-instances --region eu-west-1 --query "Reservations[].Instances[?InstanceId==\`$ID\`].Tags[]") done
i-60****2a
[ [ { "Value": "SB", "Key": "Name" } ] ]
i-1a****56
[ [ { "Value": "Course and Schedule on Apache 2.4", "Key": "Name" } ] ]
i-88eff3cb
[ [ { "Value": "secret.com", "Key": "Name" } ] ]
i-e7****a5
[ [ { "Value": "2014.03 + Docker", "Key": "Name" } ] ]
I could not find a way to do that with only one AWS CLI call. Should someone come up with a shorter solution, I would love to hear from you.
If you want to filter to certain tag key/value only, you can edit the aws ec2 describe-instances to add a --filter option.
For example :
aws ec2 describe-instances --region eu-west-1 --filter "Name=tag:Name,Values=SB"
--Seb
simple answer:
aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].[InstanceId,Tags[*]]'
not so simple answer .. the same principle could be applied to both instances and vpc's
# how-to get all the tags per instance-id with aws ec2 for prd env
while read -r i ; do \
aws ec2 describe-tags --filters "Name=resource-id,Values=$i" \
--profile prd; done < <(aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].InstanceId' --profile prd)
# how-to get all the tags per virtual-private-cloud-id
while read -r v; do aws ec2 describe-tags \
--filters "Name=resource-id,Values=$v" ; \
done < <(aws ec2 describe-vpcs \
--query 'Vpcs[*].VpcId'|perl -ne 's/\t/\n/g;print')