AWS-cli ec2 describe instances
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)'
chris evans
Updated on November 04, 2020Comments
-
chris evans over 3 years
i am digging on aws cli and from past 5 hours i struggling with 2 cli commands
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)
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 over 4 yearsaws ec2 describe-instances --filters "Name=tag:Name,Values=instance_name" | jq .Reservations[0].Instances[0].InstanceId
-
adavea about 4 yearsFor 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 over 2 yearsis there any way i can filter if tag value is not equal to some value?
-
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 about 2 yearsThank you, I couldn't find that in AWS documentation