Use AWS CLI and JQ to get a list of instances sorted by launch time

aws ec2 describe-instances | jq '.["Reservations"]|.[]|.Instances|.[]|.LaunchTime + " " + .InstanceId' | sort -n
Use the AWS CLI tools to generate a list instances, then pipe them to JQ to show only their launch time and instance id. Finally use sort to bring them out in runtime order. Find all those instances you launched months ago and have forgotten about.
Sample Output
"2014-01-30T12:44:57.000Z i-xxxxxxxx"
"2014-01-31T08:19:33.000Z i-xxxxxxxx"
"2014-01-31T08:19:38.000Z i-xxxxxxxx"
"2014-01-31T12:33:27.000Z i-xxxxxxxx"
"2014-01-31T12:33:27.000Z i-xxxxxxxx"
"2014-01-31T14:50:01.000Z i-xxxxxxxx"
"2014-01-31T14:50:06.000Z i-xxxxxxxx"

2014-02-03 07:59:47

1 Alternatives + Submit Alt

What Others Think

How would you go about listing instances in certain VPC with the Tag Name, private IP address of instance and instance id? The closest I have get is using the following commands. This manage to lists all name of instances. aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-e2f17e8b --query 'Reservations[].Instances[].Tags[?Key==`Name`].Value[]' This manage to list all private ip address, instance id and ALL tags which I don't need. I just need the name. aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-e2f17e8b | jq '.Reservations[].Instances[] | {PrivateIpAddress, InstanceId, Tags}' I'm not sure why I can't execute command like yours in this way: aws ec2 describe-instances | jq '.["Reservations"]|.[]|.Instances|.[]|.PrivateIpAddress + " " + .InstanceId + " " + .Tags[?Key==`Name`].Value[]' This works but its showing all the Tags Key names aws ec2 describe-instances | jq '.["Reservations"]|.[]|.Instances|.[]|.PrivateIpAddress + " " + .InstanceId + " " + .Tags'
imagineer · 404 weeks and 1 day ago
Here you go(even has your vpc-id so you should be able to just run this): aws ec2 describe-instances --filters "Name=vpc-id,Values=vpc-e2f17e8b" "Name=instance-state-name,Values=running" --query 'Reservations[].Instances[].[ [Tags[?Key==`Name`].Value][0][0],PrivateIpAddress,InstanceId ]' --output table Note: 1. I have an extra filter in there that only queries for running instances. You can change that to whatever you'd like. 2. You can add on to the query portion if you'd like other information (e.g., remove the filter of instance-state-name, and instead add on after the instance-id section this: State.Name). You could also add things like the AZ , e.g., aws ec2 describe-instances --filters "Name=vpc-id,Values=vpc-e2f17e8b" "Name=instance-state-name,Values=running" --query 'Reservations[].Instances[].[ [Tags[?Key==`Name`].Value][0][0],PrivateIpAddress,InstanceId,State.Name,Placement.AvailabilityZone ]' --output table
fydgit · 326 weeks and 1 day ago
Err, fixing that last one to be what I said: aws ec2 describe-instances --filters "Name=vpc-id,Values=vpc-e2f17e8b" --query 'Reservations[].Instances[].[ [Tags[?Key==`Name`].Value][0][0],PrivateIpAddress,InstanceId,State.Name,Placement.AvailabilityZone ]' --output table Also, you can change the "--output" to be any of the following: - table - text - json Each are useful in their own right.
fydgit · 326 weeks and 1 day ago
derp, one last try: aws ec2 describe-instances --filters "Name=vpc-id,Values=vpc-e2f17e8b" --query 'Reservations[].Instances[].[ [Tags[?Key==`Name`].Value][0][0],PrivateIpAddress,InstanceId,State.Name,Placement.AvailabilityZone ]' --output table
fydgit · 326 weeks and 1 day ago
It is very useful thanx
jothibasu · 290 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: