How to Use the AWS Price List API with Examples
If you're building a business application that requires up-to-date AWS pricing, the AWS Price List API is for you.
By Pilotcore
AWS Price List API Overview
The AWS Price List API is designed to help you programmatically access AWS service pricing information. The API provides two primary versions:
- AWS Price List Bulk API (2015): This version allows you to download large, structured JSON files containing pricing data.
- AWS Price List Query API (2017): A RESTful API that supports granular querying, metadata retrieval, and filtering capabilities, ideal for customized pricing queries.
AWS Price List Bulk API
Introduction
The AWS Price List Bulk API is accessible via simple HTTP GET requests and returns bulk pricing information in JSON format. This version does not require AWS account credentials and can be accessed publicly.
Endpoints
- Primary Endpoint:
https://pricing.us-east-1.amazonaws.com
Key URL Structures
-
Service List: To get a list of all services with pricing data:
https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/index.json
-
Specific Service Pricing: For instance, to get pricing data for Amazon EC2 instances in the US East (N. Virginia) region:
https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/us-east-1/index.json
Data Structure
The response JSON from the Bulk API contains:
formatVersion
: The version of the JSON format.disclaimer
: AWS’s legal disclaimer regarding pricing.publicationDate
: When the pricing data was published.offers
: A dictionary of services (offerCode
) and URLs to access detailed pricing for each service.
Example Use Case with wget
and jq
You can retrieve and parse data using tools like wget
and jq
for command-line processing:
wget -q -O - https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/ca-central-1/index.json | jq -r '.products[].sku'
This command downloads the JSON data for Amazon EC2 in the Canada (Central) region and extracts the SKU of each instance type.
AWS Price List Query API
Introduction
The AWS Price List Query API provides a more flexible and advanced way to interact with pricing data. It allows you to filter prices by region, usage type, and other attributes, making it suitable for dynamic applications.
Endpoints
The Price List Query API is available at the following regional endpoints:
https://api.pricing.us-east-1.amazonaws.com
https://api.pricing.eu-central-1.amazonaws.com
https://api.pricing.ap-south-1.amazonaws.com
IAM Permissions
To use the Query API, you need to have permissions defined in your IAM policy. Here’s an example of a policy that grants necessary access:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"pricing:DescribeServices",
"pricing:GetAttributeValues",
"pricing:GetProducts"
],
"Resource": "*"
}
]
}
Main API Functions
-
DescribeServices: Retrieves a list of available services and their attributes, which you can use to filter your pricing queries.
-
GetAttributeValues: Fetches possible values for a specific attribute key, such as
instanceType
for EC2. -
GetProducts: Returns filtered pricing data based on criteria such as region, product family, and usage type.
Example: Using Python with boto3
Here’s a Python script demonstrating how to use the AWS SDK to query pricing information:
import boto3
from botocore.config import Config
# Configuration for the AWS Pricing API client
my_config = Config(
region_name='us-east-1',
retries={'max_attempts': 10}
)
client = boto3.client('pricing', config=my_config)
# Retrieve service details
response = client.describe_services(ServiceCode='AmazonEC2')
print(response)
This script connects to the AWS Pricing API and fetches service details for Amazon EC2.
Using the AWS CLI with aws pricing
The AWS CLI provides a dedicated command to interact with AWS pricing data: aws pricing
. This command allows you to query pricing information directly from the terminal, leveraging the same capabilities as the AWS Price List Query API.
AWS CLI Setup
Ensure the AWS CLI is installed and configured with your credentials. You can install the AWS CLI via pip:
pip install awscli
Configure the CLI:
aws configure
Key Commands
-
Describe Services: Retrieve information about AWS services that have pricing data available.
aws pricing describe-services --region us-east-1
This command lists all services with available pricing data.
-
Get Attribute Values: Fetch attribute values that can be used to filter pricing queries, such as
instanceType
.aws pricing get-attribute-values --service-code AmazonEC2 --attribute-name instanceType --region us-east-1
-
Get Products: Query specific pricing details. You can filter the data using parameters like service code, region, or product family.
Example to get EC2 pricing:
aws pricing get-products --service-code AmazonEC2 --filters Type=TERM_MATCH,Field=location,Value="US East (N. Virginia)" --region us-east-1
This command retrieves the pricing details of Amazon EC2 in the specified region.
Example: Query EC2 On-Demand Pricing
Here’s a command that fetches the On-Demand pricing for EC2 instances in the US East (N. Virginia) region:
aws pricing get-products \
--service-code AmazonEC2 \
--filters Type=TERM_MATCH,Field=location,Value="US East (N. Virginia)" \
--filters Type=TERM_MATCH,Field=productFamily,Value="Compute Instance" \
--region us-east-1 \
--output json | jq '.PriceList[] | fromjson | .terms.OnDemand[] | .priceDimensions[] | .pricePerUnit.USD'
Output Format
- JSON: Default and highly structured, containing detailed pricing information.
- CSV: You can format the data into CSV using custom scripts or command-line tools like
jq
for easier analysis.
Benefits of Using AWS CLI for Pricing
- Flexibility: You can script complex queries, making it easy to automate pricing checks.
- Integration: Easily integrates into existing AWS workflows or CI/CD pipelines.
- Customization: Filter results based on multiple parameters, getting only the data you need.
Conclusion
The AWS Price List APIs and AWS CLI tools provide robust options for accessing AWS pricing data programmatically. Whether you need bulk data or refined queries, these tools can help you retrieve pricing information for better cost management and analysis.
For further details, visit the official AWS Pricing API Documentation.
Ready to Elevate Your Business?
Discuss your cloud strategy with our experts and discover the best solutions for your needs.