Created
March 7, 2019 09:54
-
-
Save delijati/5c4e53f9933aeafffcd507fba2f308aa to your computer and use it in GitHub Desktop.
This file contains 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
# 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