Monday, May 9, 2016

Filtering tags from the AWS CLI

There are a number of ways that i have seen to filter resource tags and i thought i better record them quick..

Here we filter for the tag called Name

You can do it simple,  Name=tag:Name

aws ec2 describe-instances --filter Name=tag:Name,Values=WHATEVS --query 'Reservations[*].Instances[*].InstanceId' --output text

You can get the same effect using key and value so...

aws ec2 describe-instances --filter  Name=tag-key,Values=name Name=tag-value,Values=WHATEVS --query 'Reservations[*].Instances[*].InstanceId' --output text

also - --filter and --filters both work the same

if you want to call describe tags instead of describe resources..

aws --output=text ec2 describe-tags --filters Name=resource-id,Values=i-1234566 Name=key,Values=Name

escaping back ticks in bash is also bitch for JMESPath query searches

these work..

take note of the use of ' vs " in two querys

' means strictly read the characters, " means operate on characters like $ if you see them.

       # killme=`aws --region $AWSREGION ec2 describe-volumes --query 'Volumes[*].VolumeId' --filters "Name=tag:TerminationDate,Values="\`echo $todaysdate\`"" "Name=status,Values=available" --output text`

snaps_to_delete=`$AWS ec2 describe-images --image-ids $ami_ids_to_delete --owners self --query "Images[?contains(Tags[].Value, "'\`'$tag'\`'") == "'\`true\`'"]|[].[BlockDeviceMappings[].Ebs[].SnapshotId]" --filters Name=tag-key,Values="TerminationDate" Name=tag-value,Values=$terminationdate --output text`






No comments:

Post a Comment