Finding all Amazon AWS Instances That Do Not Have a Certain Tag

前端 未结 9 2017
借酒劲吻你
借酒劲吻你 2020-12-23 12:09

I\'m trying to use the Amazon AWS Command Line Tools to find all instances that do not have a specified tag.

Finding all instances WITH a tag is simple enough, e.g.<

9条回答
  •  时光取名叫无心
    2020-12-23 12:40

    Since --filters parameter doesn't seem to support inverse filtering, here's my solution to this problem using --query parameter:

    aws ec2 describe-instances \
    --query 'Reservations[].Instances[?!contains(Tags[].Key, `Name`)][].InstanceId'
    

    It looks at an array of tag keys for each instance and filters those instance that don't have Tag 'Name' in the array. Then flattens output to array of instance IDs.

    • Advantage over some previous answers: no need for jq or other command to filter output.
    • Disadvantage over true inverse filter: likely to be much slower over large number of instances.

提交回复
热议问题