# -*- coding: utf-8 -*- import commands, boto3 TARGET_INSTANCE_NAME = "***" # Enter your target Instance Name GEM_NUM = 2 # 保存世代数 def _(cmd): return commands.getoutput(cmd) def handler(event, context): images = boto3.client('ec2').describe_images(Owners=["self"])["Images"] delete_count = len(images) - GEM_NUM targetImages = [] for image in sorted(images, key=lambda x: x["CreationDate"]): if (delet