forked from crowdsecurity/jupyter-ecs-spawner
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen_resource_files.py
75 lines (64 loc) · 2.63 KB
/
gen_resource_files.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
import json
import boto3
def get_aws_regions():
'''Return all AWS regions'''
ec2 = boto3.client('ec2')
describe_args = {}
ret = []
while True:
describe_result = ec2.describe_regions(**describe_args)
for region in describe_result['Regions']:
ret.append(region['RegionName'])
if 'NextToken' not in describe_result:
break
describe_args['NextToken'] = describe_result['NextToken']
return ret
def get_ec2_instance_types(region):
'''Return all EC2 instance types in a region'''
ec2 = boto3.client('ec2', region_name=region)
describe_args = {}
ret = {}
while True:
describe_result = ec2.describe_instance_types(**describe_args)
for instance_type in describe_result['InstanceTypes']:
instance = {'vcpu': instance_type['VCpuInfo']['DefaultVCpus'], 'memory': instance_type['MemoryInfo']['SizeInMiB'],
'arch': instance_type['ProcessorInfo']['SupportedArchitectures'][0]}
if instance_type.get('GpuInfo'):
instance['gpu'] = {'count': instance_type['GpuInfo']["Gpus"][0]["Count"],
"memory": instance_type['GpuInfo']['TotalGpuMemoryInMiB'],
'type': instance_type['GpuInfo']["Gpus"][0]["Name"]}
ret[instance_type['InstanceType']] = instance
if 'NextToken' not in describe_result:
break
describe_args['NextToken'] = describe_result['NextToken']
return ret
def get_ecs_optimized_ami(region):
ssm = boto3.client('ssm', region_name=region)
ret = {}
for type in ['amd', 'arm64', 'gpu']:
if type == 'amd':
r = ssm.get_parameters(Names=['/aws/service/ecs/optimized-ami/amazon-linux-2/recommended'])['Parameters']
else:
r = ssm.get_parameters(Names=['/aws/service/ecs/optimized-ami/amazon-linux-2/{0}/recommended'.format(type)])['Parameters']
try:
value = json.loads(r[0]['Value'])
except IndexError:
print(f"Could not find AMI for {type} in region {region}")
continue
ret[type] = value['image_id']
return ret
regions = get_aws_regions()
amis = {}
instances = {}
for region in regions:
print(f"Getting data for {region}")
#print("Available instance types in {}".format(region))
instances[region] = get_ec2_instance_types(region)
amis[region] = get_ecs_optimized_ami(region)
with open('ecsspawner/amis.json', 'w') as f:
json.dump(amis, f)
with open('ecsspawner/instances.json', 'w') as f:
json.dump(instances, f)
with open('ecsspawner/regions.json', 'w') as f:
json.dump(regions, f)