chew224
chew224

Reputation: 501

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?

Upvotes: 7

Views: 21832

Answers (2)

Maverick
Maverick

Reputation: 748

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.

Upvotes: 4

Mark B
Mark B

Reputation: 200486

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.

Upvotes: 13

Related Questions