-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate_mappings.py
49 lines (46 loc) · 1.93 KB
/
update_mappings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# this script is a kludge, but it works.
import boto3
import pprint
import yaml
#import all the regions
ec2 = boto3.client('ec2')
response = ec2.describe_regions()
mappings = {"AWSAMIRegionMap":{}}
for region in response['Regions']:
#for region in [{"RegionName": "us-east-1"},{"RegionName": "us-east-2"}]:
# search for ARM images for AMZ Linux 2
ec2_request = boto3.client('ec2', region_name=region['RegionName'])
print (region['RegionName'])
arch = 'arm64'
candidates = ec2_request.describe_images(
Filters=[
{'Name': 'virtualization-type', 'Values': ['hvm']},
{'Name': 'architecture', 'Values': [arch]},
{'Name': 'state', 'Values': ['available']},
{'Name': 'root-device-type', 'Values': ['ebs']},
{'Name': 'image-type', 'Values': ['machine']},
{'Name': 'owner-alias', 'Values': ['amazon']},
{'Name': 'name', 'Values': ['amzn2-ami-hvm-*']}
]
)
instanceType = "t4g"
if len(candidates["Images"]) == 0:
arch = 'x86_64'
instanceType = "t3"
candidates = ec2_request.describe_images(
Filters=[
{'Name': 'virtualization-type', 'Values': ['hvm']},
{'Name': 'architecture', 'Values': [arch]},
{'Name': 'state', 'Values': ['available']},
{'Name': 'root-device-type', 'Values': ['ebs']},
{'Name': 'image-type', 'Values': ['machine']},
{'Name': 'owner-alias', 'Values': ['amazon']},
{'Name': 'name', 'Values': ['amzn2-ami-hvm-*']}
]
)
#sort the candidates for the region
if len(candidates["Images"]) != 0:
candidates['Images'].sort(key=lambda x: x['CreationDate'], reverse=True)
mappings["AWSAMIRegionMap"][region['RegionName']] = {"AMI": candidates['Images'][0]["ImageId"], "type": instanceType}
print()
print (yaml.dump({"Mappings": mappings}))