Interview Prep/Python for DevOps/boto3 & AWS automation

How would you find the latest AMI programmatically?

IntermediateConceptboto3 & AWS automation
Answer

The cleanest way is the public SSM parameter:

ami = ssm.get_parameter(
    Name="/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64")["Parameter"]["Value"]

For your own golden AMIs, filter and sort:

imgs = ec2.describe_images(Owners=["self"], Filters=[{"Name": "tag:Role", "Values": ["web"]}])["Images"]
latest = max(imgs, key=lambda i: i["CreationDate"])["ImageId"]

CreationDate is an ISO string, so lexical max works.

All Python for DevOps questions Roadmap: Python for automation

More on boto3 & AWS automation