Skip to content

Instantly share code, notes, and snippets.

@RantyDave
Last active October 28, 2017 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RantyDave/54284f4fb58b83617fc90237d27beed8 to your computer and use it in GitHub Desktop.
Save RantyDave/54284f4fb58b83617fc90237d27beed8 to your computer and use it in GitHub Desktop.
Produces a price list for EC2 instances in the current region
# 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']))
@RantyDave
Copy link
Author

RantyDave commented Oct 6, 2017

     t2.nano $0.007/hr $   5/mo $ 0.007/ecu $ 0.015/GB vcpu= 1 ecu=  1.0 ram=  0.5 network=Low
    t2.micro $0.015/hr $  11/mo $ 0.015/ecu $ 0.015/GB vcpu= 1 ecu=  1.0 ram=  1.0 network=Low to Moderate
    t2.small $0.029/hr $  21/mo $ 0.029/ecu $ 0.015/GB vcpu= 1 ecu=  1.0 ram=  2.0 network=Low to Moderate
   t2.medium $0.058/hr $  43/mo $ 0.058/ecu $ 0.015/GB vcpu= 2 ecu=  1.0 ram=  4.0 network=Low to Moderate
    t2.large $0.117/hr $  85/mo $ 0.117/ecu $ 0.015/GB vcpu= 2 ecu=  1.0 ram=  8.0 network=Low to Moderate
    m4.large $0.125/hr $  91/mo $ 0.019/ecu $ 0.016/GB vcpu= 2 ecu=  6.5 ram=  8.0 network=Moderate
    c4.large $0.130/hr $  95/mo $ 0.016/ecu $ 0.035/GB vcpu= 2 ecu=  8.0 ram= 3.75 network=Moderate
    r4.large $0.160/hr $ 117/mo $ 0.001/ecu $ 0.010/GB vcpu= 2 ecu=135.0 ram=15.25 network=Up to 10 Gigabit
   t2.xlarge $0.234/hr $ 171/mo $ 0.234/ecu $ 0.015/GB vcpu= 4 ecu=  1.0 ram= 16.0 network=Moderate
   m4.xlarge $0.250/hr $ 182/mo $ 0.019/ecu $ 0.016/GB vcpu= 4 ecu= 13.0 ram= 16.0 network=High
   c4.xlarge $0.261/hr $ 191/mo $ 0.016/ecu $ 0.035/GB vcpu= 4 ecu= 16.0 ram=  7.5 network=High
   r4.xlarge $0.319/hr $ 233/mo $ 0.319/ecu $ 0.010/GB vcpu= 4 ecu=  1.0 ram= 30.5 network=Up to 10 Gigabit
  t2.2xlarge $0.467/hr $ 341/mo $ 0.467/ecu $ 0.015/GB vcpu= 8 ecu=  1.0 ram= 32.0 network=Moderate
  m4.2xlarge $0.500/hr $ 365/mo $ 0.019/ecu $ 0.016/GB vcpu= 8 ecu= 26.0 ram= 32.0 network=High
  c4.2xlarge $0.522/hr $ 381/mo $ 0.017/ecu $ 0.035/GB vcpu= 8 ecu= 31.0 ram= 15.0 network=High
  r4.2xlarge $0.638/hr $ 466/mo $ 0.012/ecu $ 0.010/GB vcpu= 8 ecu= 53.0 ram= 61.0 network=Up to 10 Gigabit
   d2.xlarge $0.870/hr $ 635/mo $ 0.062/ecu $ 0.029/GB vcpu= 4 ecu= 14.0 ram= 30.5 network=Moderate
  m4.4xlarge $1.000/hr $ 730/mo $ 0.019/ecu $ 0.016/GB vcpu=16 ecu= 53.5 ram= 64.0 network=High
  c4.4xlarge $1.042/hr $ 761/mo $ 0.017/ecu $ 0.035/GB vcpu=16 ecu= 62.0 ram= 30.0 network=High
  r4.4xlarge $1.277/hr $ 932/mo $ 0.013/ecu $ 0.010/GB vcpu=16 ecu= 99.0 ram=122.0 network=Up to 10 Gigabit
   p2.xlarge $1.542/hr $1126/mo $ 0.129/ecu $ 0.025/GB vcpu= 4 ecu= 12.0 ram= 61.0 network=High
  d2.2xlarge $1.740/hr $1270/mo $ 0.062/ecu $ 0.029/GB vcpu= 8 ecu= 28.0 ram= 61.0 network=High
  c4.8xlarge $2.085/hr $1522/mo $ 0.016/ecu $ 0.035/GB vcpu=36 ecu=132.0 ram= 60.0 network=10 Gigabit
 m4.10xlarge $2.500/hr $1825/mo $ 0.020/ecu $ 0.016/GB vcpu=40 ecu=124.5 ram=160.0 network=10 Gigabit
  r4.8xlarge $2.554/hr $1864/mo $ 0.365/ecu $ 0.010/GB vcpu=32 ecu=  7.0 ram=244.0 network=10 Gigabit
  d2.4xlarge $3.480/hr $2540/mo $ 0.062/ecu $ 0.029/GB vcpu=16 ecu= 56.0 ram=122.0 network=High
 m4.16xlarge $4.000/hr $2920/mo $ 0.021/ecu $ 0.016/GB vcpu=64 ecu=188.0 ram=256.0 network=20 Gigabit
 r4.16xlarge $5.107/hr $3728/mo $ 0.189/ecu $ 0.010/GB vcpu=64 ecu= 27.0 ram=488.0 network=20 Gigabit
 hs1.8xlarge $5.570/hr $4066/mo $ 0.159/ecu $ 0.048/GB vcpu=17 ecu= 35.0 ram=117.0 network=10 Gigabit
  d2.8xlarge $6.960/hr $5081/mo $ 0.060/ecu $ 0.029/GB vcpu=36 ecu=116.0 ram=244.0 network=10 Gigabit
 g3.16xlarge $7.016/hr $5122/mo $ 0.037/ecu $ 0.014/GB vcpu=64 ecu=188.0 ram=488.0 network=20 Gigabit
 x1.16xlarge $9.671/hr $7060/mo $ 0.078/ecu $ 0.010/GB vcpu=64 ecu=124.5 ram=976.0 network=High
  p2.8xlarge $12.336/hr $9005/mo $ 0.131/ecu $ 0.025/GB vcpu=32 ecu= 94.0 ram=488.0 network=10 Gigabit
 x1.32xlarge $19.341/hr $14119/mo $ 0.055/ecu $ 0.010/GB vcpu=128 ecu=349.0 ram=1952.0 network=High
 p2.16xlarge $24.672/hr $18011/mo $ 0.131/ecu $ 0.032/GB vcpu=64 ecu=188.0 ram=768.0 network=20 Gigabit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment