Skip to content

Instantly share code, notes, and snippets.

@marklit
Last active October 29, 2020 08:59
Show Gist options
  • Save marklit/9ec5d8fb6bdfcb376a33 to your computer and use it in GitHub Desktop.
Save marklit/9ec5d8fb6bdfcb376a33 to your computer and use it in GitHub Desktop.
Find the cheapest availability zone across all regions for an EC2 spot instance type
pip install sh
from datetime import datetime
import json
from pprint import pprint
from sh import aws
regions = [ 'ap-northeast-1',
'ap-southeast-1',
'ap-southeast-2',
'eu-central-1',
'eu-west-1',
'sa-east-1',
'us-east-1',
'us-west-1',
'us-west-2']
instance_type = 'm3.xlarge'
prices = []
start_time = datetime.utcnow().isoformat()
for region in regions:
_prices = aws('ec2',
'describe-spot-price-history',
instance_type=instance_type,
start_time='%sZ' % start_time,
product_description='Linux/UNIX',
region=region)
_prices = json.loads(str(_prices))
prices = prices + [(p['SpotPrice'], p['AvailabilityZone'])
for p in _prices['SpotPriceHistory']]
pprint(sorted(prices))
[(u'0.032700', u'us-east-1e'),
(u'0.033400', u'us-east-1a'),
(u'0.033400', u'us-east-1d'),
(u'0.034100', u'us-west-1c'),
(u'0.034500', u'us-west-1a'),
(u'0.038400', u'us-east-1c'),
(u'0.038700', u'us-west-2c'),
(u'0.040400', u'ap-northeast-1a'),
(u'0.040900', u'sa-east-1a'),
(u'0.041000', u'us-west-2a'),
(u'0.041100', u'us-west-2b'),
(u'0.041200', u'ap-northeast-1c'),
(u'0.041300', u'eu-west-1c'),
(u'0.041400', u'eu-central-1b'),
(u'0.042000', u'ap-southeast-2b'),
(u'0.042000', u'eu-west-1b'),
(u'0.042500', u'eu-west-1a'),
(u'0.043500', u'sa-east-1c'),
(u'0.044600', u'ap-southeast-1a'),
(u'0.044600', u'ap-southeast-2a'),
(u'0.045100', u'ap-southeast-1b'),
(u'0.047500', u'eu-central-1a'),
(u'0.381100', u'sa-east-1b')]
@0xnurl
Copy link

0xnurl commented Mar 29, 2018

❤️

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