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
  • autoscaling:DescribeLaunchConfigurations

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 following arguments:

  • number_of_amis_to_keep to specify how many AMIs to keep (default is 3).
  • region for the AWS region (default is us-east-1).
  • dry_run for running without deleting any resources in AWS.
  • skip_image_under_use_verification for skipping verification on whether the AMI is being used by any auto scaling group or reserved EC2 instance.