Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws describe-instances query tags

I'm trying to filter my output for describe-instances tot show the following:
- instanceId
- Device + Volume - Tag[Key==Name]

The expression I have is

 aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query "Reservations[*].Instances[].[InstanceId, BlockDeviceMappings[*].{DeviceName:DeviceName,VolumeName:Ebs.VolumeId}, Tags[*]]"

But this gives me output where all the tags are showing. How can I change this to only the "Name" tag?

like image 685
ShadowFlame Avatar asked Dec 07 '22 22:12

ShadowFlame


2 Answers

You are looking for a JMESPath Filter Expression. Try this:

aws ec2 describe-instances \
    --filters Name=instance-state-name,Values=running \
    --query 'Reservations[*].Instances[].[InstanceId, BlockDeviceMappings[*].{DeviceName:DeviceName,VolumeName:Ebs.VolumeId}, Tags[?Key==`Name`]]'
like image 77
ataylor Avatar answered Jan 06 '23 16:01

ataylor


This worked for me

aws ec2 describe-instances --filters  "Name=instance-state-name,Values=running" --query 'Reservations[].Instances[].[ [Tags[?Key==`Name`].Value][0][0],[Tags[?Key==`tag_component_name`].Value][0][0] ]' --output json --profile myprofile --region us-east-1

Make sure to change the --output, --profile and --region paramaters

like image 42
sudheerchamarthi Avatar answered Jan 06 '23 15:01

sudheerchamarthi