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.

Example

Excute the script as: 

$ ./getAMIs.sh 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 getAMIs.sh):

#!/bin/bash
# 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
NOAMI="NOT_SUPPORTED"
# 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
fi
# Check whether parameter has been provided
if [ -z "$1" ]
then
NAME=suse-sles-12-sp3-byos-v20180104-hvm-ssd-x86_64
echo "No parameter provided."
else
NAME=$1
fi
echo "Will search for AMIs with name: ${NAME}"
echo "---------------------------------------"
##NAME=suse-sles-12-sp3-byos-v20180104-hvm-ssd-x86_64
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" ]
then
AMI=$NOAMI
fi
echo "\"${i}\" : {\"HVM64\": \"${AMI}\" },"
done

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...