Trying to output only specific tag values from describe-instances using the --query
for example
aws idis-eu-west-1 ec2 describe-instances --query "Reservations[_].Instances[_].{ID:InstanceId, TAG:Tags[0].Value}"
Does not necessarily return the Value of the tag-key Name tag it would be useful if you could refer to the tags in a manner similar to a perl Hash e.g.:
aws ec2 describe-instances --filter "Name=tag-key, Values=Name" --query "Reservations[_].Instances[_].{ID:InstanceId, TAG:Tags{'Name'}.Value}"
AWS CLI version 1.3.0 now supports this. To query based on a name you could say:
aws ec2 describe-instances --query 'Reservations[].Instances[].Tags[?Key==`Name`].Value[]'
[
"name1",
"myinstance2",
...
"devinstance"
]
Thanks
thanks
Although I do not know yet how to fix it I wanted to let you know that there is a flow in this method because when are asking for keys like (Key Name) it fails.
Do you guys have any idea of how to fix that?
@jamesls I am also wondering how to preserve a nice formatting when using this.
Before I used to do this
aws ec2 describe-instances \
--instance-ids $INSTANCES_IDS \
--query "Reservations[].Instances[*].{DNS:PublicDnsName,Name:Tags[0].Value}" \
--output text
# => Tab separated
ec2-DNS1.eu-central-1.compute.amazonaws.com SomeTagValue
ec2-DNS2.eu-central-1.compute.amazonaws.com SomeOtherTagValue
Whereas the suggested syntax messes up the formatting
aws ec2 describe-instances \
--instance-ids $INSTANCES_IDS \
--query "Reservations[].Instances[*].{DNS:PublicDnsName,Name:Tags[?Key=='Name'].Value}" \
--output text
# => Outputs with line breaks :'(
NAME Name1
ec2-DNS1.eu-central-1.compute.amazonaws.com
NAME Name2
ec2-DNS2.eu-central-1.compute.amazonaws.com
Is it possible to preserve the formatting with the Tags[?Key=='Name'].Value
syntax ?
This seems to fix the formatting with the table for me...
Tags[?Key=='Name'] | [0].Value
https://github.com/aws/aws-cli/issues/1567
So if you want to output in table with Name Tag, State, PrivateIP address and placement zone below is ready recipe
aws ec2 describe-instances --filters 'Name=tag:Name,Values=*estore2*' --query 'Reservations[].Instances[].[Tags[?Key==
Name]|[0].Value,State.Name,PrivateIpAddress,Placement.AvailabilityZone]' --output table
Adding on to the last two comments, the following style will make a table with headings 'Name' and 'PublicIP', if you wanted headings.
aws ec2 describe-instances --query 'Reservations[].Instances[].{Name:Tags[?Key==`Name`]|[0].Value,PublicIP:PublicIpAddress}' --output table
Use this if you need only the tag name and its value without the additional information
aws ec2 describe-tags --filters "Name=resource-id,Values=**Instance-id**" --query 'Tags[*].{Na
me:Key,Value:Value}'
You can also specify the particular tag using its positional value. Eg: Tags[0] would return the 1st tag
here another example to select key based on value with a like (contain search) :
aws ec2 describe-subnets --filters "Name=tag:Name,Values=MainDev-P*" --query 'Subnets[*].{ID:SubnetId,Name:Tags[?Key==`Name`]|[0].Value,KubeTags:Tags[?contains(Key, `kubernetes`) == `true`]|[*].[Key,Value]}' --output table
i find it astonishing that amazon doesn't add the basic syntax for some of their cli commands.
Most helpful comment
This seems to fix the formatting with the table for me...
Tags[?Key=='Name'] | [0].Value
https://github.com/aws/aws-cli/issues/1567