Sub topic 12
Cross-checking prices with the AWS Price List API
This chapter assumes you can configure a service in the AWS Pricing Calculator, know what an AWS Region is, and can read a monthly cost breakdown. It shows how to look up list prices programmatically, so you can check a rate that appears in an estimate.
Why cross-check a price
The calculator shows an hourly or per-unit rate for each line item. Usually that rate is correct. Errors tend to come from your inputs: the wrong Region, operating system, or tenancy. A second source of the rate lets you separate "the calculator is wrong" from "I configured something different from what I meant".
The AWS Price List Query API returns the public list price for AWS products. You can call it through the AWS CLI. It returns list prices only. It does not include your discounts, credits, or Free Tier usage.
Query one EC2 configuration
The get-products command takes a service code and a set of filters. Each filter narrows the result by one product attribute. The following command asks for one Linux t3.micro On-Demand product in US East (N. Virginia).
aws pricing get-products \
--region us-east-1 \
--service-code AmazonEC2 \
--filters \
"Type=TERM_MATCH,Field=instanceType,Value=t3.micro" \
"Type=TERM_MATCH,Field=location,Value=US East (N. Virginia)" \
"Type=TERM_MATCH,Field=operatingSystem,Value=Linux" \
"Type=TERM_MATCH,Field=tenancy,Value=Shared" \
"Type=TERM_MATCH,Field=preInstalledSw,Value=NA" \
"Type=TERM_MATCH,Field=capacitystatus,Value=Used" \
--max-items 1The --region us-east-1 option selects the endpoint that answers the query. It is not the Region of the instance. The instance Region is the location filter.
Each filter matches a field of the product record. These fields line up with the choices you make in the calculator:
| Filter field | Calculator choice it matches |
|---|---|
| `instanceType` | Instance type, such as `t3.micro` |
| `location` | Region |
| `operatingSystem` | Operating system |
| `tenancy` | Shared or dedicated hardware |
| `preInstalledSw` | Pre-installed software, such as SQL Server |
| `capacitystatus` | Normal usage versus reserved capacity |
If you leave a filter out, the query can return several products that differ only in that field. You then cannot tell which record matches your estimate. Add filters until one product remains.
Read the result
The response is JSON. Each item in PriceList is itself a JSON string, so you may need to parse it a second time. The On-Demand rate is under terms, then OnDemand, then priceDimensions, in the pricePerUnit field. Its unit field tells you what the rate covers, which for EC2 is Hrs.
Compare that rate with the rate in the calculator line item. If they match, your inputs describe the product you intended. If they differ, check each filter field against your calculator configuration.
Check the arithmetic by hand
Once the rates agree, check how the calculator turned the rate into a monthly figure. The calculator multiplies rate, hours, and instance count. The function below repeats that calculation.
HOURS_PER_MONTH = 730 # average hours in a month, a common basis for monthly estimates
def monthly_compute_cost(hourly_rate, instance_count, utilization=1.0):
"""Estimate monthly On-Demand compute cost.
utilization is the fraction of the month the instances run (0 to 1).
"""
return hourly_rate * HOURS_PER_MONTH * utilization * instance_count
# hourly_rate is an illustrative placeholder, not a current AWS price.
example_rate = 0.02
print(round(monthly_compute_cost(example_rate, instance_count=3, utilization=0.5), 2))With the placeholder rate this prints 21.9. Replace example_rate with the rate from the API and compare the result with the line item.
Common errors
- Too few filters. The query returns several products, and you read the wrong one.
- Confusing the endpoint Region with the instance Region. Use
locationfor the instance. - Wrong location text. The
locationvalue is a name such asUS East (N. Virginia), not a code such asus-east-1. - Expecting discounted prices. The API returns list prices. Savings Plans, Reserved Instances, and Free Tier usage are modeled separately in the calculator.
- Forgetting that a rate is only one input. A matching rate does not validate your hours, utilization, or data volumes.
Exercise
A calculator line item shows a rate that differs from the Price List API result for the same instance type. List two filter fields you would check first, and say why the --region option is not one of them.
Show answer
Check location (the Region) and operatingSystem, because a different Region or operating system gives a different rate. Also acceptable are tenancy or preInstalledSw. The --region option only selects which API endpoint answers the query, so it does not change the product being priced.