image AWS provides an API for everything! This includes the Pricing API to find out how much you can spend via their other APIs. This API can be difficult to use to answer questions like:

How much does an EC2 instance cost?

The difficulty comes because the API has a single endpoint [GetProducts](https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_pricing_GetProducts.html) that returns all prices for every service and product. That is a ton of data that requires lots of filtering to get what you want. You can download all the EC2 price data at once from a file Amazon hosts here. This file is 510Mb!

The data can also be accessed in more hacky ways. For example, using this file that is only 124Kb. The file is used for their frontend so is in an annoying format (JSON object wrapped in JavaScript) and starts with the comment:

This file is intended for use only on aws.amazon.com. We do not guarantee its availability or accuracy.

How much does an EC2 on demand instance cost?

Lets write a python script using the Pricing API to find the cost of an EC2 instance.

To start we need to install boto with pip install boto3. The script starts with imports and a client: import boto3, json client = boto3.client('pricing') # create the client

To call the pricing API we get_products with the ServiceCode='AmazonEC2' and some filters: response = client.get_products( ServiceCode='AmazonEC2', Filters=[ { 'Type': 'TERM_MATCH', 'Field': '<field>', 'Value': '<value>' }, ... ], )

The filters we needs are:

  1. Operating System is Linux: 'Field': 'operatingSystem', 'Value': 'Linux'
  2. Cost of running the instance: 'Field': 'operation', 'Value': 'RunInstance and 'Field': 'capacitystatus', 'Value': 'Used'
  3. On a shared instance: 'Field': 'tenancy', 'Value': 'Shared'
  4. Of an instance type 'Field': 'instanceType', 'Value': '<insance_type>, e.g. r4.large
  5. In a region 'Field': 'location', 'Value': '<region>'. An annoying part of the pricing API is it uses non-standard region IDs, e.g. use US East (N. Virginia) instead of us-east-1.

The top level element is the PriceList: price_list = response["PriceList"]

Because of the filters, this list should only contain one “unstructured” JSON object that can be parsed: price_item = json.loads(price_list[0])

This returns an object like: { "serviceCode": "AmazonEC2", "product": { "productFamily": "Compute Instance", "sku": "CGJXHFUSGE546RV6" "attributes": { "memory": "15.25 GiB", "vcpu": "2", ... } }, "terms": { "OnDemand": { "CGJXHFUSGE546RV6.JRTCKXETXF": { ... "priceDimensions": { "CGJXHFUSGE546RV6.JRTCKXETXF.6YS6EN2CT7": { "unit": "Hrs", ... "pricePerUnit": { "USD": "0.1330000000" } } } } }, "Reserved": { ... } } }

To get the price data we need to do some digging: ``terms = price_item[“terms”]
term = terms[“OnDemand”].itervalues().next()

price_dimension = term[“priceDimensions”].itervalues().next()
price = price_dimension[‘pricePerUnit’][“USD”]

print price

0.1330000000``

That is how to get the price for an EC2 instance in AWS.

More answers are here, here and here.