AWS CLI – How to list instances and dump two specific tags along with other items

amazon-web-servicesaws-cli

The cli below works perfectly now I would also like to capture two other tags in addition to 'Name' called 'Application' and 'Environment'. How can I change this to add Application and Environment tags to the out-put.

aws ec2 describe-instances --query "Reservations[].Instances[].
  [Placement.AvailabilityZone,InstanceId,InstanceType,Platform,
   State.Name,PrivateIpAddress,StateTransitionReason,Tags[?Key=='Name'] 
   | [0].Value]" --output table

Best Answer

You can accomplish this by adding filters to your query:

aws ec2 describe-instances --query "Reservations[].Instances[].
  [Placement.AvailabilityZone,InstanceId,InstanceType,Platform,
   State.Name,PrivateIpAddress,StateTransitionReason]" 
    --filters "Name=tag:Name,Values=my-name" "Name=tag:env,Values=prod" 
     --output table"

This will return only the instances tagged with Name == my-name and env == prod

Related Topic