As cloud engineers, it’s easy to spin up infrastructure fast but equally easy to forget that every running resource costs money. Tracking AWS spend manually is tedious, and cost surprises at the end of the month are no fun.
That’s what inspired this project: an AWS Cost Optimization Dashboard that automatically checks daily spend and sends an alert to Slack when costs exceed a defined threshold. The setup uses Terraform for infrastructure, AWS Lambda for automation, and AWS Cost Explorer for cost visibility.
Project Overview
The goal was to create a lightweight automation that:
Fetches daily AWS spend from Cost Explorer.
Stores detailed cost data in S3.
Sends an alert to Slack when spend crosses a set threshold.
Keeps everything defined in Terraform for repeatable deployment.
Tech Stack
1.AWS Lambda – runs cost-check logic daily.
2.AWS Cost Explorer API – provides service-level spend data.
3.AWS S3 – stores daily cost data as JSON files.
4.AWS Secrets Manager – securely stores the Slack webhook URL.
- Terraform – provisions and connects all AWS resources.
6.Slack – receives daily or threshold-based alerts.
Step 1: Project Structure
Here’s the basic file layout:
aws-cost-optimization-dashboard/
│
├── lambda/
│ └── handler.py
│
├── scripts/
│ └── upload_lambda.sh
│
├── main.tf
├── variables.tf
└── outputs.tf
Each part has a clear role:
main.tf provisions AWS infrastructure.
lambda/handler.py contains the logic to query AWS costs and post to Slack.
scripts/upload_lambda.sh zips and uploads the Lambda code to S3.
variables.tf and outputs.tf manage configuration and outputs cleanly.
Step 2: Terraform Setup
Terraform provisions:
An S3 bucket for storing cost data.
A Lambda function.
An IAM role for the Lambda to access Cost Explorer, Secrets Manager, and S3.
A CloudWatch Event Rule to trigger the Lambda daily.
Once the files are ready, initialize and deploy:
- terraform init
- terraform apply -auto-approve
Step 3: Lambda Function
The Lambda script does four main things:
Fetches daily spend from AWS Cost Explorer.
Saves that data to S3.
Compares total spend against a threshold.
Sends a Slack alert if the threshold is exceeded.
Example logic snippet:
if total_cost > threshold:
slack_message = {
"text": f"AWS Cost Alert: ${total_cost:.2f} exceeded threshold of ${threshold:.2f}"
}
http.request("POST", webhook_url, body=json.dumps(slack_message), headers={"Content-Type": "application/json"})
This keeps notifications clean and meaningful.
Step 4: Slack Integration
The Slack webhook URL is stored securely in AWS Secrets Manager. The Lambda retrieves it at runtime and posts messages using the Slack API.
A typical Slack alert looks like this:
AWS Cost Alert
Date: 2025-10-04
Total Spend: $12.47
Top Service: Amazon EC2 ($8.30)
Threshold: $10.00
Step 5: Deploying Lambda
The helper script makes packaging easy:
#!/bin/bash
set -e
cd ../lambda
zip -r ../lambda_function.zip .
aws s3 cp ../lambda_function.zip s3://aws-lambda-artifacts-opuba/lambda/cost_function.zip --region us-east-1
echo "Lambda package uploaded successfully."
Make it executable:
chmod +x scripts/upload_lambda.sh
./scripts/upload_lambda.sh
Then rerun Terraform to update Lambda:
terraform apply -auto-approve
Step 6: Testing
You can trigger the Lambda manually from the AWS Console or run it via CLI:
aws lambda invoke --function-name cost-optimization-dashboard /tmp/output.json
If the cost exceeds the set limit, you’ll instantly see an alert in Slack.
Step 7: Results and Takeaways
This project taught me that cost visibility doesn’t need complex dashboards or third-party tools. With a few AWS-native services and Terraform, you can build your own automated alert system that scales easily.
Cloud engineers can expand this by:
Adding service-specific thresholds.
Tracking weekly or monthly trends.
Integrating with AWS Budgets or Grafana dashboards.
GitHub Repository
Find the full source code and setup guide here
https://github.com/Copubah/aws-cost-optimization-dashboard
Need Help Implementing This?
If your company wants to set up automated AWS cost monitoring and alerts, I can help you implement this exact solution. I also provide targeted support to tailor it to your organization’s specific needs and budget.
Reach out through this form for a quick consultation:
https://docs.google.com/forms/d/e/1FAIpQLSfFxxikFTPySZD2Zo8xB2c_2X1YGAJvhSLtJqetNuNRRfv9ow/viewform?usp=sharing&ouid=106342220232180156406
Top comments (0)