Last active
July 10, 2017 18:04
-
-
Save ozgurakan/58a6f20fb3e395510b453dda6158eb32 to your computer and use it in GitHub Desktop.
Creates mapping in YAML for AWS CloudFormation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: