AWS AMI Cleanup

This gem provides the cleanup_amis script that allows deregistering unused AMIs. The IAM user running the command must have at the very least the following permissions:

  • ec2:DescribeImages
  • ec2:DescribeLaunchTemplateVersions
  • ec2:DescribeInstances
  • ec2:DeregisterImage
  • ec2:DeleteSnapshot
  • autoscaling:DescribeAutoScalingGroups

Script should be invoked as follows:

cleanup_amis clean_amis --ami_name 'my-ami' --ami_owner 'self'

Where ami_owner can be a combination of AWS account IDs, self, amazon, and aws-marketplace.

Additionally you can provide the number_of_amis_to_keep argument to specify how many AMIs to keep (default is 3) and region for the AWS region (default is us-east-1).