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