Skip to content

Instantly share code, notes, and snippets.

@daleobrien
Last active December 20, 2015 21:24
Show Gist options
  • Save daleobrien/6181760 to your computer and use it in GitHub Desktop.
Save daleobrien/6181760 to your computer and use it in GitHub Desktop.
Quick script to get the costs for EC2 instances for Sydney only. Needs 2 installs to work, pip install requests pip install workbook
#!/usr/bin/env python
'''
'''
try:
import requests
except ImportError:
print 'You need to install "requests". Run "pip install requests" '
try:
from workbook import print_table
except ImportError:
print 'You need to install "workbook". Run "pip install workbook" '
HOURS_IN_A_YEAR = 8765.81
DETAILS = {
# '': [ECU, Name, RAM, Note]
't1.micro': [2, 'Micro Instance', '613 MiB', 'Up to 2 EC2 Compute Units (for short periodic bursts)'],
'm1.small': [1, 'General purpose', '1.7 GiB', '1 virtual core with 1 EC2 Compute Unit'],
'm1.medium': [2, '', '3.75 GiB', '1 virtual core with 2 EC2 Compute Unit'],
'm1.large': [4, '', '7.5 GiB', '2 virtual cores with 2 EC2 Compute Units each'],
'm1.xlarge': [8, '', '15 GiB', '4 virtual cores with 2 EC2 Compute Units each'],
'm3.xlarge': [13, 'General purpose', '15 GiB', '4 virtual cores with 3.25 EC2 Compute Units each'],
'm3.2xlarge': [26, '', '30 GiB', '8 virtual cores with 3.25 EC2 Compute Units each'],
'm2.xlarge': [6.5, 'Memory Optimized', '17.1 GiB', '2 virtual cores with 3.25 EC2 Compute Units each'],
'm2.2xlarge': [13, '', '34.2 GiB', '4 virtual cores with 3.25 EC2 Compute Units each'],
'm2.4xlarge': [26, '', '68.4 GiB', '8 virtual cores with 3.25 EC2 Compute Units each'],
'c1.medium': [5, 'Compute optimized', '1.7 GiB', '2 virtual cores with 2.5 EC2 Compute Units each'],
'c1.xlarge': [20, '', '7 GiB', '8 virtual cores with 2.5 EC2 Compute Units each'],
'c3.large': [7, 'Compute optimized', '3.5 GiB', '2 virtual cores with 2 x 16GB SSD'],
'c3.xlarge': [14, '', '7 GiB', '4 virtual cores with 2 x 40GB SSD'],
'c3.2xlarge': [28, '', '15 GiB', '8 virtual cores with 2 x 80GB SSD'],
'c3.4xlarge': [55, '', '20 GiB', '16 virtual cores with 2 x 160GB SSD'],
'c3.8xlarge': [108, '', '60 GiB', '32 virtual cores with 2 x 320GB SSD'],
'hs1.8xlarge': [35, 'Storage optimized', '117 GiB', '16 virtual cores with 24 * 2048 GB HD'],
'hi1.4xlarge': [35, '', '60.5 GiB', '16 virtual cores with 2 * 1024GB SSD'],
'g2.2xlarge': [26, 'GPU', '15 GiB', '8 virtual cores with 60GB SSD'],
'i2.xlarge': [14, 'Storage Optimized', '30.5 GiB', '4 virtual cores with 800GB SSD'],
'i2.2xlarge': [27, '', '61 GiB', '8 virtual cores with 2 x 800GB SSD'],
'i2.4xlarge': [53, '', '122 GiB', '16 virtual cores with 4 x 800GB SSD'],
'i2.8xlarge': [104, '', '244 GiB', '32 virtual cores with 8 x 800GB SSD'],
}
#instances_spot_price = {}
#url = 'https://console.aws.amazon.com/ec2/spot/price-history?instanceType="c3.large"&productDescription="Linux/UNIX"&startTime="2013-12-23T00:10:00"&endTime="2013-12-23T00:10:00"&maxResults=200&mbtc=397228822&region=ap-southeast-2'
#
#params = {'instanceType': '"c3.large"',
# 'productDescription': '"Linux/UNIX"',
# 'startTime': '"2013-12-23T00:10:00"',
# 'endTime': '"2013-12-23T00:10:00"',
# 'maxResults': 200,
# 'region': 'ap-southeast-2',
# 'mbtc':397228822
#}
#data = requests.get("https://console.aws.amazon.com/ec2/spot/price-history",
# params=params)
#print data
#print data.json()
#exit(0)
instances_ondemand = {}
url = 'http://aws.amazon.com/ec2/pricing/json/linux-od.json'
data = requests.get(url)
for r in data.json()['config']['regions']:
if r['region'] in ('apac-syd', ):
for instance_type in r['instanceTypes']:
for s in instance_type['sizes']:
ec2_size = s['size']
values = s['valueColumns']
for value in values:
p = value['prices']['USD']
if p == 'N/A':
continue
instances_ondemand[ec2_size] = p
url = 'http://aws.amazon.com/ec2/pricing/json/linux-ri-heavy.json'
data = requests.get(url)
instances_reserved = {}
for r in data.json()['config']['regions']:
if r['region'] in ('ap-southeast-2', ):
for instance_type in r['instanceTypes']:
for s in instance_type['sizes']:
ec2_size = s['size']
values = s['valueColumns']
row = []
yr1 = 0.0
yr3 = 0.0
hr1 = 0.0
hr3 = 0.0
for value in values:
p = value['prices']['USD']
if p == 'N/A':
continue
name = value['name']
if name == 'yrTerm1':
yr1 = float(p)
if name == 'yrTerm3':
yr3 = float(p)
if name == 'yrTerm1Hourly':
hr1 = float(p)
if name == 'yrTerm3Hourly':
hr3 = float(p)
if yr1:
m1 = (yr1 + HOURS_IN_A_YEAR * hr1) / 12.0
m3 = (yr3 / 3.0 + HOURS_IN_A_YEAR * hr3) / 12.0
m1 = "$ %0.2f" % m1
m3 = "$ %0.2f" % m3
instances_reserved[ec2_size] = (m1, m3)
def cmp(x, y):
def _cmp(_x, _y):
if _x > _y:
return 1
elif _x == _y:
return 0
return -1
a, b = x.split('.')
c, d = y.split('.')
if a != c:
order = ('t1', 'c1', 'c2', 'c3', 'm1', 'm2', 'm3', 'hs1')
if a in order and c in order:
i = order.index(a)
j = order.index(c)
return _cmp(i, j)
return _cmp(x, y)
else:
order = ('tiny', 'small', 'medium', 'large',
'xlarge', '2xlarge', '4xlarge', '8xlarge')
if b in order and d in order:
i = order.index(b)
j = order.index(d)
return _cmp(i, j)
return _cmp(b, d)
keys = list(set(instances_ondemand.keys() + instances_reserved.keys()))
keys.sort(cmp=cmp)
data = [['Type', 'Hourly', 'EC2/$', 'Monthly (1yr)', 'Monthly (3yr)',
'EC2', '', 'RAM', 'Details']]
blank_row = ['' for cell in data[0]]
last_key = None
for key in keys:
row = [key]
if key.split('.')[0] != last_key: # and last_key != None:
data.append(blank_row)
last_key = key.split('.')[0]
if key in instances_ondemand:
row.append(instances_ondemand[key])
else:
row.append(0)
if key in instances_reserved:
x = DETAILS[key][0] / float(row[-1])
row.append("%0.3f" % x)
row += instances_reserved[key]
else:
row.append('-')
row.append([0, 0])
row += DETAILS[key]
data.append(row)
print_table(data, 'Sydney EC2 Prices')
@daleobrien
Copy link
Author

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