Skip to content

Instantly share code, notes, and snippets.

@brunodasilvalenga
Created June 4, 2020 06:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brunodasilvalenga/5b4356fbe792556a1a91183fe2805c0f to your computer and use it in GitHub Desktop.
Save brunodasilvalenga/5b4356fbe792556a1a91183fe2805c0f to your computer and use it in GitHub Desktop.
Script python to get spot price with last 4 hours.
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