Hello,
When I am trying to list instance names in text format it works
aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==Name].Value[]]' --output text
However when I try to list the same as table it returns an error.
aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==Name].Value[]]' --output table
Error - Row should have 1 elements, instead it has 0
My eventual goal is to list all stopped instances with details of instance-id, Name-tag and instance type in a tabular format.
The command I am trying is:
aws ec2 describe-instances --query 'Reservations[].Instances[].[ InstanceId,[Tags[?Key==Name].Value][0][0],State.Name,InstanceType,Placement.AvailabilityZone ]' --output table
Any help would be appreciated.
There are a couple issues with your query:
Tags[?Key==Name]
should be Tags[?Key==\'Name\']
-- notice that we are using literal syntax here.[0][0]
from the list, you can stop the projection and take the first result using the |
character.Does the following expression work correctly for you?
aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==
"Name"].Value | [0], State.Name, InstanceType, Placement.AvailabilityZone]' --output table
Thanks for the response @mtdowling. I tried the expression shared by you. It gives me the following output
| DescribeInstances |
+------------+-------+----------+--------------+--------------+
|
|
|
It still does not give me the "Name" tag
I got the output that I am looking for in "text" format using the below expression
aws ec2 describe-instances --query 'Reservations[_].Instances[_].[Placement.AvailabilityZone,InstanceId,InstanceType,Platform,State.Name,Tags[?Key ==Name
]]' --output text
However, when I change the output format from text to table it gives an error as "list index out of range". Any suggerstions?
Sorry for the delay. Here's an expression you can use:
$ aws ec2 describe-instances --query 'Reservations[].Instances[].[Placement.AvailabilityZone,InstanceId,InstanceType,Platform,State.Name,Tags[?Key==`Name`] | [0].Value]' --output table
----------------------------------------------------------------------------------------------
| DescribeInstances |
+------------+-------------+------------+----------+----------+------------------------------+
| us-west-2b| i-12345678 | m3.large | windows | stopped | first-name-test-1 |
| us-west-2c| i-22345678 | t2.medium | None | running | second-name-test-1 |
| us-west-2a| i-32345678 | c3.large | None | stopped | third-name-test-1 |
| us-west-2b| i-42345678 | m3.xlarge | windows | stopped | fourth-name-test-1 |
| us-west-2c| i-52345678 | m3.medium | windows | running | fifth-name-test-1 |
+------------+-------------+------------+----------+----------+------------------------------+
Let me know if you have any more questions.
Hi jamesls,
i was googling for something i'm trying to do and found this forum and thought it was helpful.i'm very new to aws and mostly use it via console.one of the things i have to do is to go in to the console and see if there are any instances that are stopped for more than a week and terminate them.i normally do it from the console manually.i've never used cli before and just started reading it.is there a way that can be done.
list the availability zone,instance ID,size,state=stopped,name of the instance,date when stopped and if it is equal to 7 then terminate it.
any help/advice will be gratly appreciated.
thanks in advance,
svuppala01.
Most helpful comment
Sorry for the delay. Here's an expression you can use:
Let me know if you have any more questions.