How AWS Billed me $400 Despite Free Credits, So I Built a Daily Cost Tracker

I thought I was safe. I had AWS free credits, a modest ECS cluster, and a budget alert set at 75%. Everything felt under control, until I got slapped with a $400 surprise bill!
Turns out, the ECS cluster kept running quietly for 10 days, and since it hadn’t crossed the alert threshold, AWS didn’t notify me. My credits were drained before I even realized something was off.
That disaster led me to build my own automated cost tracker using AWS Lambda, a simple setup that emails me daily with my exact AWS spending. No more guesswork, no more surprises. Just peace of mind!
If you’re using AWS credits or juggling multiple services, this script could save you too.
Non-medium member, Click here
Services Involved
You only need three native AWS services for this streamlined solution:
1. Amazon EventBridge: Schedules and triggers your cost-checking workflow on a regular (daily) basis.
2. AWS Lambda: Runs code to calculate the previous day’s AWS cost.
3. Amazon SNS (Simple Notification Service): Delivers the daily summary straight to your email.
Visual Workflow
- EventBridge (daily trigger) ⟶ Lambda Function (cost calculation) ⟶ SNS Topic (email alert)
How Eventbridge, Lambda and SNS are connected to each other?
How does Lambda tracks per day cost for your AWS account?
We are using Python’s boto3 library, which provide some in-built functions, that can be used to track the AWS COST. One such function is called cost explorer.
You can use the Cost Explorer API to programmatically query your cost and usage data. You can query for aggregated data such as total monthly costs or total daily usage. You can also query for granular data.
In the cost explorer library, we will be using the get_cost_and_usage
function.
What does get_cost_and_usage
function do?
Retrieves cost and usage metrics for your account. You can specify which cost and usage-related metric that you want the request to return. For example, you can specify BlendedCosts
or UsageQuantity
.
response = client.get_cost_and_usage(
TimePeriod={
'Start': 'string',
'End': 'string'
},
Granularity='DAILY'|'MONTHLY'|'HOURLY',
Metrics=[
'string',
]
)
How to integrate boto3 with Lambda?
We will be creating a lambda function, and run a python code in with boto3 in action.
If your Lambda runtime is Python 3.11 or newer,
boto3
comes pre-installed, you can use it right away without any extra layers.
import json
import boto3
from datetime import datetime, timedelta
def lambda_handler(event, context):
client= boto3.client('ce')
start= '2025-06-01'
today= datetime.utcnow().date()
yesterday= today - timedelta(days=1)
todayplusone= today + timedelta(days=1)
response= client.get_cost_and_usage(
TimePeriod={
'Start': str(start),
'End': str(todayplusone)
},
Granularity= 'DAILY',
Metrics= ['UnblendedCost']
)
latest_cost= 0.0
for items in response['ResultsByTime']:
if items['TimePeriod']['Start'] == yesterday:
latest_cost= items['Total']['UnblendedCost']['Amount']
return {
'statusCode': 200,
'body': f"Current day: {today}\nCost for {yesterday} is ${latest_cost}."
}
How to trigger this lambda function?
Create an Eventbridge rule and configure it to integrate with the lambda function. You can follow these steps, or let yourself be creative :)
Create a SNS topic to send out the alerts ( using SNS subscription )
Add trigger and destination for the lambda function
Below are the image attached, if you want to follow along:
To Setup Eventbridge rule, follow below steps:
Step 1: Create an eventbridge rule
Step 2: Define the rate or cron expression for the frequency to trigger the rule
Step 3: Specify the Lambda function that is “target” to this rule
To Setup SNS, follow below steps:
Step 1: Fill below details, and leave the rest and blank, as they are optional
Step 2: Create a subscription mentioning your email
To Setup the Lambda function, follow below steps:
Step 1: Create a lambda function with below highlighted configuration
Step 2: Enter the code into the lambda function and deploy the lambda
Why Use This Method?
Minimal setup: Only three AWS services, no complex integrations.
Timely alerts: Daily breakdowns help spot issues before free credits are drained.
Cost-effective: All services are within the free tier or incur negligible charges for low-volume use.
With this setup, you’ll never be surprised by AWS costs again, and tracking your credits becomes effortless.
Follow for More
If this helped or sparked an idea, drop a comment, a clap, or reach out!
Subscribe to my newsletter
Read articles from Aakash Choudhary directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
