简体   繁体   中英

Enumerating aws tags with aws cli

I was thinking I could add a tag to my AWS machines, for example

Key: Backup Value: 00 04 * * *

and then from our management server use this to schedule volume snapshots at certain times. So far I'm struggling with jmespath stuff, and I can't seem to get my head around the json path extraction syntax:

aws ec2 describe-instances --query 'Reservations[].Instances[].Tags[?Key==`Backup`]'
[
   [],
   [],
   [],
   [],
   [
       {
           "Value": "00 04 * * *",
           "Key": "Backup"
       }
   ],
   [],
   []
]

I could use some help to work out how to extract the following two values, provided the Backup tag is defined for the instance.

InstanceId, Value of the Backup key

Try just querying the tags, specifying that you only want tags for instances, and that you want the value of the field "key" to be "Backup". (Example below)

aws ec2 describe-tags --query "Tags[*].{Backup:Value,InstanceId:ResourceId}" --filters "Name=resource-type, Values=instance" --filters "Name=key,Values=Backup" --output table 

(the output in this example is table for easy viewing of results, but you can change it to json or text as needed.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM