Friday, June 13, 2025

Automating AWS Cost Retrieval Using Python and Boto3


Effectively managing AWS costs demands accurate and timely data. Python, combined with AWS’s powerful boto3 library, makes it straightforward to programmatically access up-to-date pricing data directly from AWS. Below you'll find a clear, practical guide on retrieving AWS service pricing, with Amazon EBS snapshot costs provided as an illustrative example.


Understanding Boto3

Boto3 is the official Python Software Development Kit (SDK) provided by AWS. It simplifies interactions with various AWS services, enabling automation of management tasks, including pulling accurate cost data. Specifically, the Boto3 pricing client taps directly into AWS Pricing APIs, offering easy access to pricing information.

Motivation for Enhancing Lambda Functions with Pricing

The initial version of FinOps Lambda functions effectively identified potential cost-saving resources like idle instances or unused snapshots. However, these scripts lacked the ability to quantify savings in actual financial terms, making cost optimization less tangible. By integrating real-time pricing data from AWS, these scripts now calculate precise savings figures, dramatically improving their usefulness and enabling better financial decision-making.

Step-by-Step Guide: Retrieving AWS Pricing

Here’s a detailed walkthrough of how to fetch pricing data using Python:

Step 1: Initialize the Boto3 Pricing Client

Begin by creating a Boto3 client specific to AWS Pricing:

import boto3
import json
import logging

logger = logging.getLogger()

pricing_client = boto3.client('pricing', region_name='us-east-1')

Step 2: Retrieve Pricing Information

Use the get_products method from the Pricing client, specifying key details such as service, product type, and location. This method returns comprehensive pricing data in JSON format:

response = pricing_client.get_products(
    ServiceCode='AmazonEC2',
    Filters=[
        {'Type': 'TERM_MATCH', 'Field': 'productFamily', 'Value': 'Storage Snapshot'},
        {'Type': 'TERM_MATCH', 'Field': 'location', 'Value': 'US East (N. Virginia)'},
        {'Type': 'TERM_MATCH', 'Field': 'volumeApiName', 'Value': 'Snapshot'}
    ],
    MaxResults=1
)

Step 3: Parse and Extract Pricing Data

The response from AWS is structured as nested JSON data. Below is a simplified example of the JSON returned:

{
  "terms": {
    "OnDemand": {
      "termId1": {
        "priceDimensions": {
          "dimensionId1": {
            "pricePerUnit": {
              "USD": "0.05"
            }
          }
        }
      }
    }
  }
}

Extracting the pricing detail from this JSON involves:

product = json.loads(response['PriceList'][0])
on_demand = list(product['terms']['OnDemand'].values())[0]
price_dimensions = list(on_demand['priceDimensions'].values())[0]
price_per_unit = float(price_dimensions['pricePerUnit']['USD'])

Here, the Python script first parses the JSON into a Python dictionary, navigates to the nested terms, selects the first OnDemand pricing available, and finally extracts the pricePerUnit.

Full Example Function

Here’s a fully encapsulated Python function demonstrating the steps described above:

def fetch_aws_pricing(service_code, product_family, region, api_name):
    try:
        pricing_client = boto3.client('pricing', region_name='us-east-1')
        response = pricing_client.get_products(
            ServiceCode=service_code,
            Filters=[
                {'Type': 'TERM_MATCH', 'Field': 'productFamily', 'Value': product_family},
                {'Type': 'TERM_MATCH', 'Field': 'location', 'Value': region},
                {'Type': 'TERM_MATCH', 'Field': 'volumeApiName', 'Value': api_name}
            ],
            MaxResults=1
        )
        product = json.loads(response['PriceList'][0])
        on_demand = list(product['terms']['OnDemand'].values())[0]
        price_dimensions = list(on_demand['priceDimensions'].values())[0]
        price_per_unit = float(price_dimensions['pricePerUnit']['USD'])
        return price_per_unit
    except Exception as e:
        logger.warning(f"Could not fetch price. Error: {e}")
        return None

Practical Use Cases for Retrieved Pricing Data

Leveraging accurate, real-time pricing data helps automate several crucial cost management tasks:

  • Cost Estimation: Evaluate anticipated expenses before provisioning AWS resources.

  • Reporting and Communication: Automatically populate cost-saving reports into Amazon S3 or communication platforms like Microsoft Teams.

  • Enhanced Decision-making: Enable resource management decisions based on precise cost analysis.

By incorporating AWS Pricing data directly into your automation scripts, your organization can significantly enhance financial visibility and facilitate smarter, data-driven cost control.

Further Reading and Resources:

No comments:

Post a Comment