Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. ec2_spot_price_list.py - Display the price summary of AWS EC2's spot instances across all regions
  4. and availability zones.
  5.  
  6. Basic usage:
  7. $ pip install numpy click boto3
  8. $ python ec2_spot_price_list.py --instance-type m5.xlarge --product-description "Linux/UNIX (Amazon VPC)"
  9.  
  10. Copyright (C) 2019, Ikuya Yamada
  11. """
  12.  
  13. import collections
  14. import boto3
  15. import click
  16. import numpy as np
  17.  
  18.  
  19. @click.command()
  20. @click.option('-t', '--instance-type', default=['m5.xlarge'], multiple=True)
  21. @click.option('-d', '--product-description', default=['Linux/UNIX (Amazon VPC)'], multiple=True)
  22. def main(instance_type, product_description):
  23. client = boto3.client('ec2', 'us-east-1')
  24. regions = [o['RegionName'] for o in client.describe_regions()['Regions']]
  25. price_data = collections.defaultdict(list)
  26. for region in regions:
  27. client = boto3.client('ec2', region)
  28. data = client.describe_spot_price_history(InstanceTypes=instance_type,
  29. ProductDescriptions=product_description)
  30. for item in data['SpotPriceHistory']:
  31. key = (item['AvailabilityZone'], item['InstanceType'], item['ProductDescription'])
  32. price_data[key].append(float(item['SpotPrice']))
  33.  
  34. for (zone, ins, desc), price_list in sorted(price_data.items(), key=lambda o: o[1][0]):
  35. print(f'Availability zone {zone}')
  36. print(f'Instance type {ins}')
  37. print(f'Current price {price_list[0]:.2f}')
  38. print(f'Average price {np.mean(price_list):.2f}')
  39. print(f'Max price {max(price_list):.2f}')
  40. print('---')
  41.  
  42.  
  43. if __name__ == '__main__':
  44. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement