Cleaning up AMIs
Costs are like fingernails. You have to cut them constantly. When working with AWS, cleaning up unused resources is crucial. Otherwise, you will end up with a steadily growing AWS bill and waste money.
Do you build AMIs automatically, for example, with Packer? Learn how to automatically clean up unused AMIs to reduce EBS storage consumption and costs.
About a year ago, the tool we used to remove unused AMIs stopped working. As we could not find a working alternative, we started the project aws-amicleaner.
How does the aws-amicleaner command line tool work?
- Include AMIs by name or tag.
- Exclude AMIs in use, younger than N days, or the newest N images.
- Manually confirm the list of AMIs for deletion.
- Delete AMIs and linked EBS snapshots.
Examples
Requires Node.js >= 18.
The following example shows how to delete all AMIs in eu-west-1
matching the following conditions:
- Name starts with
amiprefix-
- Older than five days
- Not in use by EC2 instances, ASGs, Launch Configurations, and Launch Templates.
Also, keep the newest three images that match the conditions.
npx aws-amicleaner --region eu-west-1 --include-name 'amiprefix-*' --exclude-newest 3 --exclude-days 5 --exclude-in-use --verbose |
The following listing shows a typical confirmation screen.
+-----------+-------------+----------------------+--------------------------+---------+-----------------+-------------------------+ |
The following example shows how to delete AMIs from all regions that start with eu-west-*
, which resolves to eu-west-1
, eu-west-2
, and eu-west-3
those days. Also, the example filters AMIs by tag key CostCenter
and tag value X342-*1111
and the age of 7 days. Besides that the aws-amicleaner keeps the newest five images matching those conditions.
npx aws-amicleaner --region 'eu-west-*' --include-tag-key CostCenter --include-tag-value 'X342-*-1111' |
Looking for a way to run the aws-amicleaner in an automated way. Use the --force-delete
option to skip the manual approval. We use GitHub Actions to remove unused AMIs once per month.
npx aws-amicleaner --region 'eu-west-*' --include-tag-key CostCenter --include-tag-value 'X342-*-1111' --force-delete |
Arguments
Want more? Here is a list of all available arguments supported by aws-amicleaner.
-h, --help show this help message and exit |
Summary
The aws-amicleaner is a small but helpfull tool to delete unused AMIs and the corresponding EBS snapshots to reduce costs.
It’s simple to get started (requires Node.js >= 18).
npx aws-amicleaner --help |