Last active
October 28, 2017 02:26
-
-
Save RantyDave/54284f4fb58b83617fc90237d27beed8 to your computer and use it in GitHub Desktop.
Produces a price list for EC2 instances in the current region
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
# David Preece, 2017 | |
# In the public domain, provided "as is", author disclaims all warranties yah de yah de yah | |
# pip3 import requests | |
import json | |
import requests | |
def float_ecu(ecu): | |
if ecu == "Variable": | |
return 1 | |
return float(ecu) | |
def float_memory(mem): | |
mem = mem.replace(',', '') | |
return float(mem) | |
dynamic_data_text = requests.get('http://169.254.169.254/latest/dynamic/instance-identity/document').text | |
dd = json.loads(dynamic_data_text) | |
url = 'https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/%s/index.json' % dd['region'] | |
pl_text = requests.get(url).text | |
pl = json.loads(pl_text) | |
terms = {p[0]: list(p[1].values())[0]['priceDimensions'] for p in pl['terms']['OnDemand'].items()} | |
instances = [(p['attributes']['instanceType'], p['sku'], p['attributes']) for p in pl['products'].values() | |
if 'instanceType' in p['attributes'] and | |
't1' not in p['attributes']['instanceType'] and | |
'c1' not in p['attributes']['instanceType'] and | |
'm1' not in p['attributes']['instanceType'] and | |
'm2' not in p['attributes']['instanceType'] and | |
p['attributes']['tenancy'] == 'Shared' and | |
'SSD' not in p['attributes']['storage'] and | |
p['attributes']['operatingSystem'] == 'Linux'] | |
priced = [(p[0], float(list(terms[p[1]].values())[0]['pricePerUnit']['USD']), p[2]) | |
for p in instances if p[1] in terms] | |
for p in list(sorted(priced, key=lambda p: p[1])): | |
if float_ecu(p[2]['ecu']) == 0 or float_memory(p[2]['memory'][:-4]) == 0: | |
continue | |
print("%12s $%5.3f/hr $%4.0f/mo $%6.3f/ecu $%6.3f/GB vcpu=%2s ecu=%5.1f ram=%5s network=%s" % | |
(p[0], p[1], p[1] * 730, | |
p[1] / float_ecu(p[2]['ecu']), p[1] / float_memory(p[2]['memory'][:-4]), | |
p[2]['vcpu'], float_ecu(p[2]['ecu']), float_memory(p[2]['memory'][:-4]), p[2]['networkPerformance'])) |
Author
RantyDave
commented
Oct 6, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment