Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS-cli ec2 describe instances

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

like image 815
chris evans Avatar asked Nov 27 '22 10:11

chris evans


1 Answers

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)'
like image 182
Ali Avatar answered Dec 09 '22 11:12

Ali