Skip to content

Instantly share code, notes, and snippets.

@cmcconnell1
Last active August 10, 2020 20:45
Show Gist options
  • Save cmcconnell1/52a1f8adfcb7afa884648438e8748f79 to your computer and use it in GitHub Desktop.
Save cmcconnell1/52a1f8adfcb7afa884648438e8748f79 to your computer and use it in GitHub Desktop.
example using lyfts awspricing module to get global AWS region EC2 on-demand and reserved instance pricing
#!/usr/bin/env python3
import boto3
import awspricing
import os
# example using lyfts awspricing module to get global AWS region EC2 on-demand and reserved instance pricing
# created as answer/response for another user on realypython slack.
# ./simple-awspricing.py
# global_regions: ['eu-north-1', 'ap-south-1', 'eu-west-3', 'eu-west-2', 'eu-west-1', 'ap-northeast-2', 'ap-northeast-1', 'sa-east-1', 'ca-central-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-central-1', 'us-east-1', 'us-east-2', 'us-west-1', 'us-west-2']
# REGION: us-east-1 ON-DEMAND-PRICE: 0.085 RESERVED-PRICE: 0.037987823439878235
# REGION: us-east-2 ON-DEMAND-PRICE: 0.085 RESERVED-PRICE: 0.03775951293759513
# REGION: us-west-1 ON-DEMAND-PRICE: 0.106 RESERVED-PRICE: 0.05217960426179604
# REGION: us-west-2 ON-DEMAND-PRICE: 0.085 RESERVED-PRICE: 0.037987823439878235
# ...
# AWSPRICING_USE_CACHE: Whether to use a simple file-based cache. Valid values are 0 | 1. Defaults to 0 (false).
os.environ["AWSPRICING_USE_CACHE"] = "1"
# AWSPRICING_CACHE_PATH: Prefix to write cache files. Defaults to /tmp/awspricing.
os.environ["AWSPRICING_CACHE_PATH"] = "/var/tmp/awspricing"
# AWSPRICING_CACHE_MINUTES: Number of minutes to keep cache for. Defaults to 1440 (1 day).
os.environ["AWSPRICING_CACHE_MINUTES"] = "1440"
client = boto3.client('ec2')
global_regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
print("global_regions: ", global_regions)
for region in global_regions:
ec2_offer = awspricing.offer('AmazonEC2')
ondemand_region_price = ec2_offer.ondemand_hourly(
'c5.large',
operating_system='Linux',
region=region
)
reserved_region_price = ec2_offer.reserved_hourly(
'c5.large',
operating_system='Linux',
lease_contract_length='3yr',
offering_class='convertible',
purchase_option='Partial Upfront',
region=region
)
print("REGION: ", region, " ", "ON-DEMAND-PRICE: ", ondemand_region_price, " ", "RESERVED-PRICE: ", reserved_region_price)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment