AWS CloudFormation (Resource Collection)

AWS CloudFormation (Resource Collection) Stefan Schneider Wed, 02/20/2019 - 10:08

Get AMI Ids for all world wide AWS regions

Get AMI Ids for all world wide AWS regions

AWS CloudFormation is a technology which allows automated software installation in all AWS regions.

AWS CloudFormation requires a unique AMI identifier to pick the appropriate AMI. AWS is using however different AMI identifiers for the same content in all regions. A developer who wants the script to work in all regions world wide has to lookup all AMI identifiers for all regions.

The script below automates this task. It takes the AMI name as a parameter. It'll then print the output in a json format of a CloudFormation mapping.


Excute the script as: 

$ ./ suse-sles-12-sp3-byos-v20180104-hvm-ssd-x86_64

The result will be:

Will search for AMIs with name: suse-sles-12-sp3-byos-v20180104-hvm-ssd-x86_64
"eu-north-1"	: {"HVM64": "NOT_SUPPORTED" },
"ap-south-1"	: {"HVM64": "ami-0f227660" },
"eu-west-3"	: {"HVM64": "ami-5212a52f" },
"eu-west-2"	: {"HVM64": "ami-c5ccd4a1" },
"eu-west-1"	: {"HVM64": "ami-2aae3953" },
"ap-northeast-3"	: {"HVM64": "ami-4c040a31" },
"ap-northeast-2"	: {"HVM64": "ami-15ff5f7b" },
"ap-northeast-1"	: {"HVM64": "ami-caf26eac" },
"sa-east-1"	: {"HVM64": "ami-aea7e4c2" },
"ca-central-1"	: {"HVM64": "ami-4ed85d2a" },
"ap-southeast-1"	: {"HVM64": "ami-eb6f1997" },
"ap-southeast-2"	: {"HVM64": "ami-b7ec1ed5" },
"eu-central-1"	: {"HVM64": "ami-c40696ab" },
"us-east-1"	: {"HVM64": "ami-be2b7ac4" },
"us-east-2"	: {"HVM64": "ami-8fefc4ea" },
"us-west-1"	: {"HVM64": "ami-a34747c3" },
"us-west-2"	: {"HVM64": "ami-36aa004e" },

Use this output to create a CloudFormation mapping like:

"AWSRegionArch2AMI" : {
    "eu-north-1"     : {"HVM64": "NOT_SUPPORTED"},
    "ap-south-1"     : {"HVM64": "ami-0f227660" },
    "eu-west-3"      : {"HVM64": "ami-5212a52f" },
    "eu-west-2"      : {"HVM64": "ami-c5ccd4a1" },
    "eu-west-1"      : {"HVM64": "ami-2aae3953" },
    "ap-northeast-3" : {"HVM64": "ami-4c040a31" },
    "ap-northeast-2" : {"HVM64": "ami-15ff5f7b" },
    "ap-northeast-1" : {"HVM64": "ami-caf26eac" },
    "sa-east-1"      : {"HVM64": "ami-aea7e4c2" },
    "ca-central-1"   : {"HVM64": "ami-4ed85d2a" },
    "ap-southeast-1" : {"HVM64": "ami-eb6f1997" },
    "ap-southeast-2" : {"HVM64": "ami-b7ec1ed5" },
    "eu-central-1"   : {"HVM64": "ami-c40696ab" },
    "us-east-1"      : {"HVM64": "ami-be2b7ac4" },
    "us-east-2"      : {"HVM64": "ami-8fefc4ea" },
    "us-west-1"      : {"HVM64": "ami-a34747c3" },
    "us-west-2"      : {"HVM64": "ami-36aa004e" }

Important: remove the colon at the last printed line. A short coming of the current script...

The Script

Create a file with a name (like

# This script takes a a parameter which needs to be a name of an AWS AMI
# The string will have to identify the AMI uniquely in all regions.
# The script will then identify the AMI identifier in all common regions (but China)
# The script will generate an output which can be copied into json files of AWS CloudFormation
# The script has been tested on Mac OS only
# The script uses the AWS command line tools.
# The AWS command line tools have to have a default profile with the permission to
# describe a region and to describe an image

# The script can be run with normal OS user privileges.
# The script is not supposed to modify anything.
# There is no warranty. Please check the script upfront. You will use it on your own risk
# String to be used when no AMI is available in region
# Change your aws prfile if needed here:
PROFILE=" --profile default"
# Check whether AWS CLI is installed and in search path
if ! aws_loc="$(type -p "aws")" || [ -z "$aws_loc" ]; then
echo "Error: Script requeres AWS CLI . Install it and retry"
exit 1
# Check whether parameter has been provided
if [ -z "$1" ]
echo "No parameter provided."
echo "Will search for AMIs with name: ${NAME}"
echo "---------------------------------------"
R=$(aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text ${PROFILE})
for i in $R; do
AMI=`aws ec2 describe-images --output text --region $i --filters "Name=name,Values=${NAME}" ${PROFILE} | awk -F"\t" '{ print $7;}'i`
if [ -z "$AMI" ]
echo "\"${i}\" : {\"HVM64\": \"${AMI}\" },"

The script uses a variable PROFILE which is set to default. Change this variable setting if you need to use a different AWS configuration

The script isn't perfect. Please help improving it. Please post updates as comments to this page if you enhanced it.

Context needed...

This script assumes

  • that the AWS CLI is installed on the system.
  • that the AWS CLI is configured with a default profile on the system. The user of the default profile will have to have the rights to execute the describe statements.
  • that the AMI name provided is an exact match of the name. It will print "NOT_SUPPORTED" entries otherwise
  • that it can reach the Internet...


Stefan Schneider Wed, 02/20/2019 - 10:28