Created
January 14, 2016 15:53
-
-
Save robcowie/508ffd41e44b78f76b2a to your computer and use it in GitHub Desktop.
Very rough script to show EC2 spot prices
This file contains 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
# -*- coding: utf-8 -*- | |
""" | |
Requires aws and spark (the sparkline charting tool, not apache spark) | |
brew install awscli spark | |
""" | |
import argparse | |
import datetime as dt | |
import json | |
import subprocess | |
SPARK_CMD = 'spark ' | |
CMD = ' '.join(['aws ec2 describe-spot-price-history', | |
'--instance-types {instance_type}', | |
'--product-description "Linux/UNIX (Amazon VPC)"', | |
"--start-time '{start_time:%Y-%m-%dT%H:%M:%S}'"]) | |
OUTPUT_TMPL = u'{}: (min: {}, max: {} latest: {})\n{}' | |
def parse_datetime(d): | |
return dt.datetime.strptime( | |
d.replace('.000Z', ''), '%Y-%m-%dT%H:%M:%S').date() | |
def main(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('instance_type', help='i.e. r3.8xlarge') | |
parser.add_argument('--availability-zone', help='i.e. eu-central-1', | |
default=None) | |
args = parser.parse_args() | |
start_time = dt.datetime.utcnow() - dt.timedelta(days=1) | |
cmd = CMD.format(instance_type=args.instance_type, start_time=start_time, | |
availability_zone=args.availability_zone) | |
if args.availability_zone: | |
cmd = cmd + '--availability-zone "{}"'.format(args.availability_zone) | |
output = subprocess.check_output(cmd, shell=True) | |
data = json.loads(output) | |
prices = data['SpotPriceHistory'] | |
data = ((p['AvailabilityZone'], p['Timestamp'], p['SpotPrice']) | |
for p in prices) | |
# data = ((i[0], parse_datetime(i[1]), i[2]) for i in data) | |
data = ((i[0], i[2]) for i in data) | |
# Current prices | |
# Max price in the last 24hrs | |
# Min price in last 24hrs | |
# Group by AvailabilityZone | |
by_zone = {} | |
for i in data: | |
by_zone.setdefault(i[0], []).append(float(i[1])) | |
# For each region, print stats and sparkline | |
for region, prices in by_zone.iteritems(): | |
cmd = SPARK_CMD + ' '.join((str(p) for p in prices[:90])) | |
s = subprocess.check_output(cmd, shell=True).decode('utf8') | |
min_p = min(prices) | |
max_p = max(prices) | |
latest_p = prices[1] | |
print(OUTPUT_TMPL.format(region, min_p, max_p, latest_p, s)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment