A Polyglot Programmer’s Notebook

How to Easily Create Region Maps for AMIs in CloudFormation Templates

If you’ve worked with CloudFormation longer than a few hours, you’ve probably wondered how to easily create RegionMaps without manually looking up AMI Image IDs in multiple regions. This article introduces ami_lookup, a tool to do it for you.

CloudFormation is an powerful tool developed by Amazon to implement the DevOps vision of “infrastucture as code” in an AWS environment. One of the best practices for CloudFormation templates is to implement templates that are portable across regions, using a RegionMap to look up the correct Amazon Machine Image (AMI) for a given region. This is necessary because AMI IDs are not global, but are created for each region when the image is first created, or when it is copied to a new region.

A RegionMap makes it easy to create a template that works across regions, but the question of how to find the right AMI ids to put in the map is one that comes up often (for example here and here) and it’s one I wondered about myself.

Fortunately, I was wondering about it at about the same time that I was hearing about boto and boto3, the Python libraries for automating AWS the same way the AWS CLI does. So I wrote ami_lookup, a tool to do the work for you. Just pass it an AMI ID and the region where that AMI ID is located, and it will look up the corresponding ID in other regions for you.

Forks, stars, and pull requests welcome. Enjoy!