forked from vantage-sh/ec2instances.info
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2.py
277 lines (223 loc) · 10.9 KB
/
ec2.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import botocore
import boto3
import locale
import json
from pkg_resources import resource_filename
import re
import scrape
def canonicalize_location(location):
"""Ensure location aligns with one of the options returned by get_region_descriptions()"""
# The pricing API returns locations with the old EU prefix
return re.sub("^EU", "Europe", location)
# Translate between the API and what is used locally
def translate_platform_name(operating_system, preinstalled_software):
os = {'Linux': 'linux',
'RHEL': 'rhel',
'SUSE': 'sles',
'Windows': 'mswin'}
software = {'NA': '',
'SQL Std': 'SQL',
'SQL Web': 'SQLWeb',
'SQL Ent': 'SQLEnterprise'}
return os[operating_system] + software[preinstalled_software]
# Translate between the API and what is used locally
def translate_reserved_terms(term_attributes):
lease_contract_length = term_attributes.get('LeaseContractLength')
purchase_option = term_attributes.get('PurchaseOption')
offering_class = term_attributes.get('OfferingClass')
leases = {'1yr': 'yrTerm1',
'3yr': 'yrTerm3'}
options = {'All Upfront': 'allUpfront',
'Partial Upfront': 'partialUpfront',
'No Upfront': 'noUpfront'}
return leases[lease_contract_length] + str(offering_class).capitalize() + '.' + options[purchase_option]
# The pricing API requires human readable names for some reason
def get_region_descriptions():
result = dict()
# Source: https://github.com/boto/botocore/blob/develop/botocore/data/endpoints.json
endpoint_file = resource_filename('botocore', 'data/endpoints.json')
with open(endpoint_file, 'r') as f:
endpoints = json.load(f)
for partition in endpoints['partitions']:
for region in partition['regions']:
result[partition['regions'][region]['description']] = region
# The Osaka region is invite only and not in boto's list: https://github.com/boto/botocore/issues/1423
result['Asia Pacific (Osaka-Local)'] = 'ap-northeast-3'
# Alias LA local zone to its home region
result['US West (Los Angeles)'] = 'us-west-2'
return result
def get_instances():
instance_types = {}
try:
ec2_client = boto3.client('ec2', region_name='us-east-1')
ec2_pager = ec2_client.get_paginator('describe_instance_types')
instance_type_iterator = ec2_pager.paginate()
for result in instance_type_iterator:
for instance_type in result['InstanceTypes']:
instance_types[instance_type['InstanceType']] = instance_type
except botocore.exceptions.ClientError as e:
print(f"ERROR: Failure listing EC2 instance types. See README for proper IAM permissions.\n{e}")
raise e
instances = {}
pricing_client = boto3.client('pricing', region_name='us-east-1')
product_pager = pricing_client.get_paginator('get_products')
product_iterator = product_pager.paginate(
ServiceCode='AmazonEC2', Filters=[
# We're gonna assume N. Virginia has all the available types
{'Type': 'TERM_MATCH', 'Field': 'location', 'Value': 'US East (N. Virginia)'},
]
)
for product_item in product_iterator:
for offer_string in product_item.get('PriceList'):
offer = json.loads(offer_string)
product = offer.get('product')
# Check if it's an instance
if product.get('productFamily') not in ['Compute Instance', 'Compute Instance (bare metal)', 'Dedicated Host']:
continue
product_attributes = product.get('attributes')
instance_type = product_attributes.get('instanceType')
if instance_type in ['u-6tb1', 'u-9tb1', 'u-12tb1']:
# API returns the name without the .metal suffix
instance_type = instance_type + '.metal'
if instance_type in instances:
continue
new_inst = parse_instance(instance_type, product_attributes, instance_types.get(instance_type))
# Some instanced may be dedicated hosts instead
if new_inst is not None:
instances[instance_type] = new_inst
print(f"Found data for instance types: {', '.join(sorted(instances.keys()))}")
return list(instances.values())
def add_pricing(imap):
descriptions = get_region_descriptions()
pricing_client = boto3.client('pricing', region_name='us-east-1')
product_pager = pricing_client.get_paginator('get_products')
product_iterator = product_pager.paginate(
ServiceCode='AmazonEC2', Filters=[
{'Type': 'TERM_MATCH', 'Field': 'capacityStatus', 'Value': 'Used'},
{'Type': 'TERM_MATCH', 'Field': 'tenancy', 'Value': 'Shared'},
{'Type': 'TERM_MATCH', 'Field': 'licenseModel', 'Value': 'No License required'},
])
for product_item in product_iterator:
for offer_string in product_item.get('PriceList'):
offer = json.loads(offer_string)
product = offer.get('product')
product_attributes = product.get('attributes')
instance_type = product_attributes.get('instanceType')
location = canonicalize_location(product_attributes.get('location'))
# There may be a slight delay in updating botocore with new regional endpoints, skip and inform
if location not in descriptions:
print(f"WARNING: Ignoring pricing - unknown location. instance={instance_type}, location={location}")
continue
region = descriptions[location]
terms = offer.get('terms')
operating_system = product_attributes.get('operatingSystem')
preinstalled_software = product_attributes.get('preInstalledSw')
platform = translate_platform_name(operating_system, preinstalled_software)
if instance_type not in imap:
print(f"WARNING: Ignoring pricing - unknown instance type. instance={instance_type}, location={location}")
continue
# If the instance type is not in us-east-1 imap[instance_type] could fail
try:
inst = imap[instance_type]
inst.pricing.setdefault(region, {})
inst.pricing[region].setdefault(platform, {})
inst.pricing[region][platform]['ondemand'] = get_ondemand_pricing(terms)
# Some instances don't offer reserved terms at all
reserved = get_reserved_pricing(terms)
if reserved:
inst.pricing[region][platform]['reserved'] = reserved
except Exception as e:
# print more details about the instance for debugging
print(f"ERROR: Exception adding pricing for {instance_type}: {e}")
def format_price(price):
return str(float("%f" % float(price))).rstrip('0').rstrip('.')
def get_ondemand_pricing(terms):
ondemand_terms = terms.get('OnDemand', {})
price = 0.0
# There should be only one ondemand_term and one price_dimension
for ondemand_term in ondemand_terms.keys():
price_dimensions = ondemand_terms.get(ondemand_term).get('priceDimensions')
for price_dimension in price_dimensions.keys():
price = price_dimensions.get(price_dimension).get('pricePerUnit').get('USD')
return format_price(price)
def get_reserved_pricing(terms):
pricing = {}
reserved_terms = terms.get('Reserved', {})
for reserved_term in reserved_terms.keys():
term_attributes = reserved_terms.get(reserved_term).get('termAttributes')
price_dimensions = reserved_terms.get(reserved_term).get('priceDimensions')
# No Upfront instances don't have price dimension for upfront price
upfront_price = 0.0
price_per_hour = 0.0
for price_dimension in price_dimensions.keys():
temp_price = price_dimensions.get(price_dimension).get('pricePerUnit').get('USD')
if price_dimensions.get(price_dimension).get('unit') == 'Hrs':
price_per_hour = temp_price
else:
upfront_price = temp_price
local_term = translate_reserved_terms(term_attributes)
lease_in_years = term_attributes.get('LeaseContractLength')[0]
hours_in_term = int(lease_in_years[0]) * 365 * 24
price = float(price_per_hour) + (float(upfront_price)/hours_in_term)
pricing[local_term] = format_price(price)
return pricing
def parse_instance(instance_type, product_attributes, api_description):
pieces = instance_type.split('.')
if len(pieces) == 1:
# Dedicated host that is not u-*.metal, skipping
# May be a good idea to all dedicated hosts in the future
return
i = scrape.Instance()
i.api_description = api_description
i.instance_type = instance_type
i.family = product_attributes.get('instanceFamily')
if '32-bit' in product_attributes.get('processorArchitecture'):
i.arch.append('i386')
i.vCPU = locale.atoi(product_attributes.get('vcpu'))
# Memory is given in form of "1,952 GiB", let's parse it
i.memory = locale.atof(product_attributes.get('memory').split(' ')[0])
if api_description:
i.network_performance = api_description['NetworkInfo']['NetworkPerformance']
else:
i.network_performance = product_attributes.get('networkPerformance')
if product_attributes.get('currentGeneration') == 'Yes':
i.generation = 'current'
else:
i.generation = 'previous'
gpu = product_attributes.get('gpu')
if gpu is not None:
i.GPU = locale.atoi(gpu)
if api_description:
if 'FpgaInfo' in api_description:
for fpga in api_description['FpgaInfo']['Fpgas']:
i.FPGA += fpga['Count']
netinfo = api_description['NetworkInfo']
if netinfo['EnaSupport'] == 'required':
i.ebs_as_nvme = True
i.vpc = { 'max_enis': netinfo['MaximumNetworkInterfaces'],
'ips_per_eni': netinfo['Ipv4AddressesPerInterface'] }
try:
ecu = product_attributes.get('ecu')
if ecu == 'Variable':
i.ECU = 'variable'
else:
i.ECU = locale.atof(ecu)
except:
pass
i.physical_processor = product_attributes.get('physicalProcessor')
# CPU features
processor_features = product_attributes.get('processorFeatures')
if processor_features is not None:
if "Intel AVX512" in processor_features:
i.intel_avx512 = True
if "Intel AVX2" in processor_features:
i.intel_avx2 = True
if "Intel AVX" in processor_features:
i.intel_avx = True
if "Intel Turbo" in processor_features:
i.intel_turbo = True
i.clock_speed_ghz = product_attributes.get('clockSpeed')
enhanced_networking = product_attributes.get('enhancedNetworkingSupported')
if enhanced_networking is not None and enhanced_networking == 'Yes':
i.enhanced_networking = True
return i