Friday, March 7, 2014

How do I find the creation date for an AMI?

So, you have a bunch of AMIs on your AWS cloud that are just taking up space and you want to clear out the older ones. You go to sort the AMIs by date and find that there's nothing in the AWS console that lets you do that. So you look at the API and the command line client, going through a bunch of JSON and realize that there's nothing that gives a creation date for an AMI. "I just want to know when my AMI was created!" you say.

At the time of this posting, there's no way to find that. However, what you can find is a list of all your snapshots, the date they were made and the AMI they were based on. So if you've made at least one snapshot of the AMI at some point, you logically know that the AMI was created on or before that date.

Now how do we get a list of our snapshots, sorted by date or AMI? Like this:

aws ec2 describe-snapshots --owner-ids MyAWSaccountIDgoesHere --query 'Snapshots[*].[Description,StartTime]' --output text | sort -k8n,8 | cut -d' ' -f5,7 > ami-snapshot-dates_sorted-by-date.txt


If you want to get a list sorted by AMI, you'd do this:
aws ec2 describe-snapshots --owner-ids MyAWSaccountIDgoesHere --query 'Snapshots[*].[Description,StartTime]' --output text | sort -k5n,5 | cut -d' ' -f5,7 > ami-snapshot-dates_sorted-by-ami.txt


Hint: If you're feeling lazy and don't want to look up your AWS account ID, (presuming that you're the root user), you can use "--owner self" instead of "--owner-ids MyAWSaccountIDgoesHere"

No comments:

Post a Comment