7

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?

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
chew224
  • 481
  • 2
  • 6
  • 19

2 Answers2

13

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.

Mark B
  • 183,023
  • 24
  • 297
  • 295
4

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.

Maverick
  • 708
  • 9
  • 19