AWS-cli ec2 describe instances

22,186

To describe all instances with Tag "NAME" Use:

aws ec2 describe-instances --filters "Name=tag-key,Values=Name"

or

This Gives InstanceId with Particular Tag "Name"

aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==`Name`].Value[0]]'

or

This Gives InstanceId with Particular Tag "Name" and Value of Tag

aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==`Name`], Tags[?Key==`Name`].Value[]]'

To describe all instances with Tag "Purpose" and its value as "test" Use:

aws ec2 describe-instances --filters "Name=tag:Purpose,Values=test"

If you already know the Instance id:

aws ec2 describe-instances --instance-ids i-1234567890abcdef0

To find every instance which doesn't contain a tag named "Purpose":

aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(contains({Tags: [{Key: "Purpose"} ]}) | not)'

To filter against the value of the tag, instead of the name of the tag:

aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(contains({Tags: [{Key: "Name"}, {Value: "testbox1"}]}) | not)'

To find every instance which doesn't contain a tag:

aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(contains({Tags: [{Key: ""}, {Value: ""}]}) | not)'
Share:
22,186
chris evans
Author by

chris evans

Updated on November 04, 2020

Comments

  • chris evans
    chris evans over 3 years

    i am digging on aws cli and from past 5 hours i struggling with 2 cli commands

    1. i should get InstanceId, Name(this is a tag value) and specific tag value by providing its key ( including not tags given i.e NULL)

    2. I should get InstanceId, Name and specific tag value by providing its key ( excluding NULL tags)

    i got 50% of the answer for the 1 Question & 2 Question 0%

    My cli command:

    aws ec2 describe-instances --query 'jsondata[ ].Instances[ ].[InstanceId, [Tags[?keys='Name'].Value] [0][0]' --output table`

               Ex: {
    
      "Jsondata" : [
                     { "Instances" : "i-xxxxxx",
    
                       "Tags":[
    
                      { "valve":" testserver",
                          "key": "server"
                       },
                     { "valve":" elb",
                        "key": "Name"
                      }
                    ]
                    },
                 { "Instances" : "i-yyyyyy",
    
                  "Tags":[
    
                        { "valve": " ",
                           "key": " "
                         },
                     { "valve":" elb2",
                          "key": "Name"
                        }
                          ]
                       }
                    ]`
    

    Thanks in advance. Please help me i need to sleep

  • ddtraveller
    ddtraveller over 4 years
    aws ec2 describe-instances --filters "Name=tag:Name,Values=instance_name" | jq .Reservations[0].Instances[0].InstanceId
  • adavea
    adavea about 4 years
    For anyone that doesn't know what jq is, it's a command line json parsing utility that is super handy in situations like this. You can find it here: stedolan.github.io/jq/download
  • Ankit
    Ankit over 2 years
    is there any way i can filter if tag value is not equal to some value?
  • bri85
    bri85 over 2 years
    @Ankit: This answer by Ali seems to explain how to find every instance which doesn't contain a tag named "Purpose" in the third-to-last chunk of code. Perhaps that helps?
  • d3vpasha
    d3vpasha about 2 years
    Thank you, I couldn't find that in AWS documentation