Skip to content

Instantly share code, notes, and snippets.

@ikuyamada
Last active October 6, 2019 09:24
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 ikuyamada/364b29342a3dd14153eddbc6417543d8 to your computer and use it in GitHub Desktop.
Save ikuyamada/364b29342a3dd14153eddbc6417543d8 to your computer and use it in GitHub Desktop.
Display the price summary of AWS EC2's spot instances across all regions and availability zones
#!/usr/bin/env python
"""
ec2_spot_price_list.py - Display the price summary of AWS EC2's spot instances across all regions
and availability zones.
Basic usage:
$ pip install numpy click boto3
$ python ec2_spot_price_list.py --instance-type m5.xlarge --product-description "Linux/UNIX (Amazon VPC)"
Copyright (C) 2019, Ikuya Yamada
"""
import collections
import boto3
import click
import numpy as np
@click.command()
@click.option('-t', '--instance-type', default=['m5.xlarge'], multiple=True)
@click.option('-d', '--product-description', default=['Linux/UNIX (Amazon VPC)'], multiple=True)
def main(instance_type, product_description):
client = boto3.client('ec2', 'us-east-1')
regions = [o['RegionName'] for o in client.describe_regions()['Regions']]
price_data = collections.defaultdict(list)
for region in regions:
client = boto3.client('ec2', region)
data = client.describe_spot_price_history(InstanceTypes=instance_type,
ProductDescriptions=product_description)
for item in data['SpotPriceHistory']:
key = (item['AvailabilityZone'], item['InstanceType'], item['ProductDescription'])
price_data[key].append(float(item['SpotPrice']))
for (zone, ins, desc), price_list in sorted(price_data.items(), key=lambda o: o[1][0]):
print(f'Availability zone {zone}')
print(f'Instance type {ins}')
print(f'Current price {price_list[0]:.2f}')
print(f'Average price {np.mean(price_list):.2f}')
print(f'Max price {max(price_list):.2f}')
print('---')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment