Skip to content

Instantly share code, notes, and snippets.

@PauloMigAlmeida
Last active March 4, 2021 17:11
Show Gist options
  • Save PauloMigAlmeida/3faaeef3ce27fc9d68b97ae362895ac2 to your computer and use it in GitHub Desktop.
Save PauloMigAlmeida/3faaeef3ce27fc9d68b97ae362895ac2 to your computer and use it in GitHub Desktop.
Utility script that allows you to find the same ami(name) across multiple regions
#!/bin/bash
# Author: Paulo Miguel Almeida <paulo.miguel.almeida.rodenas@gmail.com>
#
# Desc: Utility script that allows you to find the same ami(name) across multiple regions.
# For instance, let's suppose that you want to find the Ubuntu Server 16.04 LTS HVM SSD
# to build a nice cloudformation template. Note that you can play around with the filters
# that AWSCLI provides. Have fun ;)
# Usage:
# find_ami_in_multiple_regions.sh ami-e93da085 (Obs.: This ami must be at same region your AWS is set)
# Output:
# ap-south-1 - ami-0355216c
# eu-west-1 - ami-0d77397e
# ap-northeast-2 - ami-8fed39e1
# ap-northeast-1 - ami-0567c164
# sa-east-1 - ami-e93da085
# ap-southeast-1 - ami-a1288ec2
# ap-southeast-2 - ami-4d3b062e
# eu-central-1 - ami-8504fdea
# us-east-1 - ami-40d28157
# us-east-2 - ami-153e6470
# us-west-1 - ami-6e165d0e
# us-west-2 - ami-a9d276c9
#
AMI_NAME=$(aws ec2 describe-images --image-ids $1 --query "Images[*].Name" --output text)
for region in $(aws ec2 describe-regions --query "Regions[*].RegionName" --output text)
do
ret=$(aws ec2 describe-images --filters "Name=root-device-type,Values=ebs" "Name=virtualization-type,Values=hvm" "Name=image-type,Values=machine" "Name=state,Values=available" "Name=name,Values=$AMI_NAME" --query "Images[*].ImageId" --output text --region $region)
echo "$region - $ret"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment