Skip to content

Instantly share code, notes, and snippets.

@pacohope
Last active September 23, 2019 15:33
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pacohope/fb573965f16c271b3604ec8d03a8dcac to your computer and use it in GitHub Desktop.
Save pacohope/fb573965f16c271b3604ec8d03a8dcac to your computer and use it in GitHub Desktop.
Given a starting public AWS AMI, generate a YAML list of AMI IDs for that AMI in all AWS regions.
#
# Given the ID of an Amazon public AMI in one region, figure out what the
# equivalent AMI IDs are for that same AMI in all other regions known.
# If that AMI isn't defined in a region, it prints the region's name, but
# comments it out.
#
from __future__ import print_function
import boto3
# Get a starting point by finding a value in the table on this page or similar page.
# https://aws.amazon.com/amazon-linux-ami/
#
# Alternatively, you can run something like:
# aws ec2 describe-images --owners amazon --filter "Name=description,Values=Amazon Linux AMI 2017*" |
# jq -c '.Images[] | {ImageId, Name, Description}'
#
# It will list images in the current region based on the filter expression. From there, you can
# figure out which image suits your needs. Stick the region and AMI identifier below.
# This is "amzn-ami-hvm-2017.03.1.20170623-x86_64-s3"
BASEAMIs= [ "ami-8c1be5f6", "ami-5648ad2c", "ami-cd0f5cb6" ]
BASEREGION="us-east-1"
# List all regions
client = boto3.client('ec2', region_name=BASEREGION)
regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
ec2 = boto3.resource('ec2', region_name=BASEREGION)
i = 0
target_image = {}
name_filter = {}
for aminame in BASEAMIs:
# Figure out what the AWS Name is for this image.
# AMI filter
image_filter = [{'Name':'image-id', 'Values':[BASEAMIs[i]]}]
image_names = list(ec2.images.filter(Filters=image_filter).all())
if( len(image_names) != 1 ):
print( "ERROR: ", len(image_names), "matched", BASEAMIs[i])
exit( 1 )
print( '# AMI{} is {}'.format( i, image_names[0].name ) )
target_image[i] = image_names[0].name
# name-based filter
name_filter[i] = [{'Name':'name', 'Values': [target_image[i]] }]
i = i+1
# Print the first lines of YAML
print( "Region2AMI:" )
# For every region, look up the AMI ID for that region by looking for
# the image with the same name.
for r in regions:
ec2 = boto3.resource('ec2', region_name=r)
i=0
print( " " + r + ":" )
while (i < len(target_image.keys()) ):
image_names = list(ec2.images.filter(Filters=name_filter[i]).all())
if( len(image_names) != 1 ):
print( '# {} undefined'.format(r) )
else:
print( ' AMI{}: {}'.format(i, image_names[0].id ) )
i=i+1
@pacohope
Copy link
Author

Example output:

Region2AMI:
  eu-west-1:
    rWebServerInstance: ami-c4bba0a2
#  ap-south-1: undefined
#  eu-west-2: undefined
  eu-west-1:
    rWebServerInstance: ami-c4bba0a2
#  ap-northeast-2: undefined
  ap-northeast-1:
    rWebServerInstance: ami-d3d3c4b4
  sa-east-1:
    rWebServerInstance: ami-1cdab170
#  ca-central-1: undefined
  ap-southeast-1:
    rWebServerInstance: ami-42901f21
  ap-southeast-2:
    rWebServerInstance: ami-43918120
  eu-central-1:
    rWebServerInstance: ami-4dbc1a22
  us-east-1:
    rWebServerInstance: ami-abc1ebbd
#  us-east-2: undefined
  us-west-1:
    rWebServerInstance: ami-347e5254
  us-west-2:
    rWebServerInstance: ami-98f3e7e1

@blancNoir
Copy link

blancNoir commented Nov 19, 2018

thanks for writing this, worked right out of the box

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment