DEV Community

Cover image for AWS Cost Management Made Easy: Build Your Own Calculator with the AWS Pricing API
Alexey Vidanov for AWS Community Builders

Posted on • Edited on • Originally published at tecracer.com

AWS Cost Management Made Easy: Build Your Own Calculator with the AWS Pricing API

When discussing AWS pricing with customers, limitations in the official AWS Pricing Calculator can sometimes hinder accurate cost estimations. This blog post addresses a specific challenge encountered with Amazon CloudWatch Synthetics pricing and provides a solution by building a custom calculator using the AWS Pricing API.

Amazon CloudWatch Synthetics is a service that allows you to create canaries—configurable scripts that run on a schedule to monitor your endpoints and APIs. One of the main challenges we faced was the need to compare different setups of CloudWatch Synthetics, such as defining the number of runs per hour and generating comparison tables—features not available in the AWS Calculator.

By following this guide, you'll learn how to create tailored calculators for any AWS service. Our solution allows flexible configurations and side-by-side comparisons, enabling better decisions when choosing the most cost-effective monitoring setup.

Note: The hosted example has been removed. Use this guide to create your own calculator or adapt it for other use cases.

Pricing Calculator Screenshot

Prerequisites

Make sure you have:

  1. An AWS account with permissions to:
    • Create/manage AWS Lambda functions
    • Access the AWS Pricing API
    • Work with Amazon S3 and Amazon CloudFront
    • Manage Amazon Route 53 (for custom domains)
  2. Basic knowledge of:
    • Python, AWS Lambda, Amazon S3
    • HTML, CSS, and JavaScript
  3. AWS CLI installed
  4. A code editor

Architecture Overview

Architecture Diagram

  • Amazon S3: Hosts the static site and pricing JSON.
  • Amazon CloudFront: Distributes content globally.
  • Amazon Route 53: Manages your domain.
  • AWS Lambda: Retrieves pricing from AWS Pricing API.
  • Amazon EventBridge: Triggers AWS Lambda function daily.

Workflow

  • AWS Lambda function fetches pricing from AWS Pricing API (available only in us-east-1)
  • JSON is stored in Amazon S3 and served via CloudFront

Example format:

{
  "us-east-1": {"USD": "0.0012000000"},
  "eu-central-1": {"USD": "0.0016000000"}
}
Enter fullscreen mode Exit fullscreen mode

Implementation Guide

AWS Lambda Function Setup

Requirements:

  • IAM permissions: Pricing API, S3 write, CloudWatch logs
  • Set env var: s3_bucket

Lambda function source: GitHub – Lambda function code

Key Function Steps

  1. Initialize
import json, boto3, os
Enter fullscreen mode Exit fullscreen mode
  1. Extract prices
def extract_prices(data):
    prices = {}
    for item in data:
        region = item['product']['attributes']['regionCode']
        terms = item['terms']['OnDemand']
        for val in terms.values():
            for dim in val['priceDimensions'].values():
                prices[region] = dim['pricePerUnit']
    return prices
Enter fullscreen mode Exit fullscreen mode
  1. Main handler
def lambda_handler(event, context):
    pricing = boto3.client('pricing', region_name='us-east-1')
    response = pricing.get_products(
        ServiceCode='AmazonCloudWatch',
        Filters=[{"Type": "TERM_MATCH", "Field": "productFamily", "Value": "Canaries"}],
        FormatVersion='aws_v1',
        MaxResults=100
    )
    prices = extract_prices([json.loads(i) for i in response['PriceList']])
    boto3.client('s3').put_object(
        Bucket=os.getenv('s3_bucket'),
        Key='canarycalc/prices.json',
        Body=json.dumps(prices)
    )
Enter fullscreen mode Exit fullscreen mode

Front-End Integration

Use the generated JSON to build a static front-end app using HTML/CSS/JS. Example code: GitHub – Frontend

Troubleshooting

  • S3 error: Check permissions
  • Pricing API failure: Validate filters and region

Resources

Final Thoughts

This basic example can be extended to any AWS service. Explore the full pricing API responses and tailor logic for your FinOps needs, including support for Savings Plans or free tier.

You can even implement pricing logic in spreadsheets or custom dashboards for business use.

AWS pricing is complex—but with automation, it's manageable.

— Alexey--

Top comments (2)

Collapse
 
jasondunn profile image
Jason Dunn [AWS]

Excellent article, nicely done!

Collapse
 
vidanov profile image
Alexey Vidanov

Thank you, Jason!