Skip to content

Instantly share code, notes, and snippets.

@delijati
Created March 7, 2019 09:54
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 delijati/5c4e53f9933aeafffcd507fba2f308aa to your computer and use it in GitHub Desktop.
Save delijati/5c4e53f9933aeafffcd507fba2f308aa to your computer and use it in GitHub Desktop.
# python spot_price.py g2.2xlarge
import sys
import boto3
import datetime
def main(argv):
if len(argv) < 2:
print("No instance provided!")
sys.exit()
client = boto3.client('ec2', region_name='us-west-2')
regions = [x["RegionName"] for x in client.describe_regions()["Regions"]]
INSTANCE = argv[1]
print("Instance: %s" % INSTANCE)
results = []
for region in regions:
client = boto3.client('ec2', region_name=region)
prices = client.describe_spot_price_history(
InstanceTypes=[INSTANCE],
ProductDescriptions=['Linux/UNIX', 'Linux/UNIX (Amazon VPC)'],
StartTime=(datetime.datetime.now() +
datetime.timedelta(days=1)).isoformat(),
MaxResults=1
)
for price in prices["SpotPriceHistory"]:
results.append((price["AvailabilityZone"], price["SpotPrice"]))
for region, price in sorted(results, key=lambda x: x[1]):
print("Region: %s spot price: %s" % (region, price))
if __name__ == "__main__":
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment