Obivan
Obivan

Reputation: 361

how can i get aws launch config from AMI iD

I have made script who clean ami id based on from not running instances. but i want also delete feature this script to clean launch config form AMI ID(actually who are not exist).

good_images = set([instance.image_id for instance in ec2.instances.all()])

#LaunchConfig in use AMI
client = boto3.client('autoscaling', region_name=region)
response = client.describe_launch_configurations()
ls_list=[]
for LC in response['LaunchConfigurations']:
    (LC['ImageId'])
print ls_list

but its not working.

Upvotes: 0

Views: 795

Answers (2)

Obivan
Obivan

Reputation: 361

used_lc = []
all_lc = []
def used_launch_config():
    for asg in client.describe_auto_scaling_groups()['AutoScalingGroups']:
        launch_config_attached_with_asg = asg['LaunchConfigurationName']
        used_lc.append(launch_config_attached_with_asg)
used_launch_config()
print used_lc

def all_spot_lc():
    for launch_config in client.describe_launch_configurations(MaxRecords=100,)['LaunchConfigurations']:
        lc = launch_config['LaunchConfigurationName']
        if str(lc).startswith("string"):
           all_lc.append(lc)
all_spot_lc()
print all_lc

I just avoid to delete launch config from AMI. Now i compared from used or unused it solved the problem. I was doing wrong in Previous code.

is there way to increase max records

Upvotes: 0

John Rotenstein
John Rotenstein

Reputation: 269520

Your code:

for LC in response['LaunchConfigurations']:
    (LC['ImageId'])

should be:

for LC in response['LaunchConfigurations']:
    (ls_list.append(LC['ImageId']))

Upvotes: 1

Related Questions