Skip to content

Instantly share code, notes, and snippets.

@Roman-Dudar
Last active December 11, 2017 11:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Roman-Dudar/8bb632e0d868e8e83f48c3939f79d95d to your computer and use it in GitHub Desktop.
Save Roman-Dudar/8bb632e0d868e8e83f48c3939f79d95d to your computer and use it in GitHub Desktop.
Get AMI to region Mapping for CloudFormation (Copy-Paste yaml)
#!/usr/bin/env bash
# Runs for ~2min
# Sample output:
# $ ./get-region-ami-mappings.sh
# ap-south-1:
# AMI: ami-d5c18eba
# eu-west-2:
# AMI: ami-e7d6c983
# eu-west-1:
# AMI: ami-1a962263
# ap-northeast-2:
# AMI: ami-1196317f
# ap-northeast-1:
# AMI: ami-da9e2cbc
# sa-east-1:
# AMI: ami-286f2a44
# ca-central-1:
# AMI: ami-d29e25b6
# ap-southeast-1:
# AMI: ami-c63d6aa5
# ap-southeast-2:
# AMI: ami-ff4ea59d
# eu-central-1:
# AMI: ami-bf2ba8d0
# us-east-1:
# AMI: ami-55ef662f
# us-east-2:
# AMI: ami-15e9c770
# us-west-1:
# AMI: ami-a51f27c5
# us-west-2:
# AMI: ami-bf4193c7
YAML_AMI_KEY="AMI"
IMAGE_SEARCH_STRING="amzn-ami-hvm-2017*gp2"
MAPPINGS=()
# by default queries all regions, adjust this array if needed
regions=($(aws ec2 describe-regions --query "Regions[].RegionName" \
--output text))
for region in ${regions[@]}
do
MAPPINGS+=("${region}:\n\t${YAML_AMI_KEY}: $(aws ec2 describe-images --owners amazon \
--filters Name=name,Values=$IMAGE_SEARCH_STRING \
--query "reverse(sort_by(Images, &CreationDate))[0].ImageId" \
--output text \
--region $region)\n")
done
echo -e $(printf %s "${MAPPINGS[@]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment