Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all AWS AMI names through CLI?

I want to write a script that prints out all the AMIs created before (or after) a certain date. However, I am really struggling to do this so any help is sincerely appreciated.

I don't have much now, but this is what I have so far:

aws ec2 describe-images > c:\ami_names.txt

Any tips on how to filter out just for the AMIs created before a certain date?

like image 295
chew224 Avatar asked Sep 28 '16 17:09

chew224


People also ask

How do I find my AMI details?

To find a Linux AMI using the AMIs page Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/ . From the navigation bar, select the Region in which to launch your instances. You can select any Region that's available to you, regardless of your location. In the navigation pane, choose AMIs.

How do you list all ec2 instances AWS CLI in all regions?

Go to VPC dashboard https://console.aws.amazon.com/vpc/home and click on Running instances -> See all regions .

What AWS CLI command is used to display all ec2 instances?

You can use the AWS CLI to list your instances and view information about them. You can list all your instances, or filter the results based on the instances that you're interested in. The following examples show how to use the aws ec2 describe-instances command. The following command lists all your instances.


2 Answers

Here's an example that queries for all images created after April 1st, 2016:

aws ec2 describe-images --query 'Images[?CreationDate>=`2016-04-01`][]'

I believe you should be able to expand on that example to get everything you need.

like image 163
Mark B Avatar answered Sep 22 '22 07:09

Mark B


You can use jq to filter the response

The command to get the AMIs created before a particular date,

aws ec2 describe-images --owners self --output json | jq '.Images[] | select(.CreationDate<'$GET_AMI') | {ImageId}' | jq --raw-output '.ImageId'))

This just gives the list of AMI-ids.

Remove | jq --raw-output '.ImageId')) if you need the json format.

Remove | {ImageId} if you need all the attributes.

like image 25
Maverick Avatar answered Sep 24 '22 07:09

Maverick