Created
June 4, 2020 06:49
-
-
Save brunodasilvalenga/5b4356fbe792556a1a91183fe2805c0f to your computer and use it in GitHub Desktop.
Script python to get spot price with last 4 hours.
This file contains hidden or 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
import boto3 | |
import datetime | |
client = boto3.client('ec2', region_name='us-west-2') | |
regions = [x["RegionName"] for x in client.describe_regions()["Regions"]] | |
print(regions) | |
INSTANCE = "t3.xlarge" | |
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(hours=4)).isoformat(), | |
MaxResults=5 | |
) | |
for price in prices["SpotPriceHistory"]: | |
print(price) | |
results.append((price["AvailabilityZone"], price["SpotPrice"])) | |
for region, price in sorted(results, key=lambda x: x[1]): | |
print("Region: %s price: %s" % (region, price)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment