Skip to content

Instantly share code, notes, and snippets.

@ozgurakan
Last active July 10, 2017 18: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 ozgurakan/58a6f20fb3e395510b453dda6158eb32 to your computer and use it in GitHub Desktop.
Save ozgurakan/58a6f20fb3e395510b453dda6158eb32 to your computer and use it in GitHub Desktop.
Creates mapping in YAML for AWS CloudFormation
# searches all regions and creates a CloudFormation "Mappings"
# YAML template with;
# - AMI IDs
# - zones for regions
#
# Usage:
# modify images dictionary
# structure is like below;
#
# 'alias': {
# 'name':'put image name that you find on AWS Console'
# 'owner-id' | 'owner-alias':'put owner-id if alias is not available'
# }
#
# You can find image name and owner-id or owner-alias by going to console
# and looking at one of the regions. Once you find these for one region
# this program can populate rest for you.
#
# You can also use AWS CLI to collect information about AMIs as seen below.
# $ aws ec2 describe-images --image-ids ami-0932686c ami-c06d48a5 --region us-east-2
#
import boto3
images = {
'MasterAMI': {
'name':'RHEL-7.3_HVM_GA-20161026-x86_64-1-Hourly2-GP2',
'owner-id':'309956199498'},
'EnterpriseAMI': {
'name':'RHEL-7.3_HVM_GA-20161026-x86_64-1-Hourly2-GP2',
'owner-id':'309956199498'},
'MinionLin': {
'name':'amzn-ami-hvm-2016.09.1.20170119-x86_64-gp2',
'owner-alias':'amazon'},
'MinionWin':{
'name':'Windows_Server-2012-R2_RTM-English-64Bit-Base-2017.05.10',
'owner-alias':'amazon'}
}
AZ_PREFIX = 'TestAZ'
client = boto3.client('ec2')
response = client.describe_regions()
regions = [region['RegionName'] for region in response['Regions']]
print('Mappings:')
print(' RegionMap:')
for region in regions:
print(' {}:'.format(region))
client = boto3.client('ec2', region_name=region)
for alias, image in images.items():
filters = []
filters.append(
{
'Name': 'name',
'Values': [
image['name']
]
})
if 'owner-id' in image:
filters.append(
{
'Name': 'owner-id',
'Values': [
image['owner-id']
]
})
if 'owner-alias' in image:
filters.append(
{
'Name': 'owner-alias',
'Values': [
image['owner-alias']
]
})
response = client.describe_images(
DryRun=False,
Filters=filters
)
for image in response['Images']:
print(' {}: {}'.format(alias, image['ImageId']))
response = client.describe_availability_zones()
zones = [zone['ZoneName'] for zone in response['AvailabilityZones'] if zone['State'] == 'available' ]
for i in range(len(zones)):
print(' {}{}: {}'.format(AZ_PREFIX, i, zones[i]))
@ozgurakan
Copy link
Author

ozgurakan commented May 24, 2017

Sample output:

Mappings:
  RegionMap:
    ap-south-1:
      MasterAMI: ami-7c9bef13
      EnterpriseAMI: ami-7c9bef13
      MinionLin: ami-f9daac96
      MinionWin: ami-67047908
      TestAz0: ap-south-1a
      TestAz1: ap-south-1b
    eu-west-2:
      MasterAMI: ami-9c363cf8
      EnterpriseAMI: ami-9c363cf8
      MinionLin: ami-f1949e95
      MinionWin: ami-07455263
      TestAz0: eu-west-2a
      TestAz1: eu-west-2b
   ....
     ....

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