Skip to content

Instantly share code, notes, and snippets.

@colebrumley
Last active October 24, 2017 18:01
Show Gist options
  • Save colebrumley/598c408b3a214ca1717e6303b2168b7b to your computer and use it in GitHub Desktop.
Save colebrumley/598c408b3a214ca1717e6303b2168b7b to your computer and use it in GitHub Desktop.
Use boto3 to pull a list of AMIs that are in use by Launch Configs that are currently assigned to an auto scaling group. Use this if you are trying to determine if it's safe to delete an AMI
import boto3
def get_amis_in_use_by_launch_configs():
asg_client = boto3.client('autoscaling')
# Pull a list of the active Launch Configs
lc_names = []
paginator = asg_client.get_paginator('describe_auto_scaling_groups')
for page in paginator.paginate():
for asg in page['AutoScalingGroups']:
if asg['LaunchConfigurationName'] not in lc_names:
lc_names.append(asg['LaunchConfigurationName'])
# Determine which AMIs are in use in active Launch Configs
all_ami_ids = []
paginator = asg_client.get_paginator('describe_launch_configurations')
for page in paginator.paginate():
for config in page['LaunchConfigurations']:
if config['LaunchConfigurationName'] not in lc_names:
continue
if config['ImageId'] not in all_ami_ids:
all_ami_ids.append(config['ImageId'])
return all_ami_ids
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment