Skip to content

Instantly share code, notes, and snippets.

@darkarnium
Last active November 21, 2016 01:02
Show Gist options
  • Save darkarnium/6b90b867111e441a03fcec5557163663 to your computer and use it in GitHub Desktop.
Save darkarnium/6b90b867111e441a03fcec5557163663 to your computer and use it in GitHub Desktop.
Fetch AWS EBS pricing and convert into JSON compatible with Django fixtures.
#!/usr/bin/env python
import re
import sys
import json
import requests
source = 'http://a0.awsstatic.com/pricing/1/ebs/pricing-ebs.min.js'
# Request.
raw = requests.get(source).text.replace('\n', '')
# 'Transform' to JSON.
x = re.search(r'^.*callback\((.*)\);', raw)
if not x:
print 'Unable to parse input data.'
sys.exit(-1)
# Ensure all keys are quoted, this is janky, but should work for at least the
# Amazon pricing data.
parsed = x.group(1)
parsed = re.sub(r'\{([\$a-zA-Z0-9\-]+)', r'{"\1", parsed)
parsed = re.sub(r',([\$a-zA-Z0-9\-]+)', r',"\1", parsed)
pricing = json.loads(parsed)
# Generate fixtures for pricing data.
fixtures = []
for region in pricing['config']['regions']:
for t in region['types']:
unit = {
'model': 'MODEL_NAME',
'fields': {
'REGION': region['region']
}
}
# Attempt to extract type from name.
name = re.search(r'\((.*)\)', t['name'])
if not name:
continue
# Attempt to extract pricing.
base_price = 0
iops_price = 0
for p in t['values']:
if p['rate'] == 'perGBmoProvStorage':
base_price = p['prices']['USD']
if p['rate'] == 'perPIOPSreq':
iops_price = p['prices']['USD']
# Add pricing unit.
unit['fields']['TYPE'] = name.group(1)
unit['fields']['PER_GB_PRICE'] = base_price
unit['fields']['PER_IOPS_PRICE'] = iops_price
fixtures.append(unit)
print json.dumps(fixtures, sort_keys=True, indent=4)
@darkarnium
Copy link
Author

Note: This doesn't cover standard volume type EBS volumes (Magnetic).

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