Last active
October 24, 2017 18:01
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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