BeginnerCloud Engineer

Set up billing alerts and cost reporting

Configure a CloudWatch billing alarm that sends an SNS notification when estimated charges exceed $100. Then write a boto3 script that queries AWS Cost Explorer for the last 30 days, and outputs spend broken down by service as a sorted table; most expensive service first.

Why this matters

Cloud costs are famously easy to run away with. A $100 alarm takes 10 minutes to set up and has saved engineers from five-figure surprise bills more than once. The Cost Explorer script gives you the visibility to understand where money is going before you get the alarm; the ideal order of operations.

Before you start

Step-by-step guide

  1. 1

    Enable billing alerts

    In the AWS Billing console, navigate to Billing Preferences and enable the Receive Billing Alerts checkbox. This must be done before CloudWatch can receive billing metrics. It can take up to 24 hours for billing data to appear; set this up first.

  2. 2

    Create an SNS topic for notifications

    Create an SNS topic called billing-alerts. Subscribe your email address to it and confirm the subscription from your inbox. CloudWatch will publish to this topic when the alarm fires; without the confirmation, no notifications will arrive.

  3. 3

    Create the CloudWatch billing alarm

    In CloudWatch, create an alarm on the EstimatedCharges metric in the AWS/Billing namespace, dimension Currency=USD. Set the threshold to 100, evaluation period to 1 day, and the action to notify the SNS topic you created. Set the alarm description; future-you will thank present-you.

  4. 4

    Write the Cost Explorer boto3 script

    Use client.get_cost_and_usage() with granularity='MONTHLY', a date range of the last 30 days, metrics=['UnblendedCost'], and group_by=[{Type: 'DIMENSION', Key: 'SERVICE'}]. Parse the response into a list of (service, cost) tuples, sort descending by cost, and print as a table.

  5. 5

    Run the script and identify your top spender

    Run the script and read the output. The top entry is almost always EC2 or S3. Add a second pass that filters to services costing more than $1 and calculates each as a percentage of total spend. This is the format a cost review meeting expects.

Relevant Axiom pages

What to do next

Back to Practice Lab