Monitoring AWS costs is essential for keeping budgets in check. In this guide, we’ll walk through creating an AWS Lambda function to retrieve cost details and send them to email (via SES) and Slack.
Prerequisites
1.AWS Account with IAM permissions for Lambda, SES, and Cost Explorer.
2.Slack Webhook URL to send messages.
3.Configured SES Email for notifications.
4.S3 Bucket for storing cost reports as CSV files.
Step 1: Enable Cost Explorer
- Go to AWS Billing Dashboard > Cost Explorer.
- Enable Cost Explorer to access detailed cost data.
Step 2: Create an S3 Bucket
- Create an S3 bucket (e.g.,
aws-cost-reports
) to store cost reports. - Ensure the bucket has appropriate read/write permissions for Lambda.
Step 3: Write the Lambda Code
1.Create a Lambda Function
- Go to AWS Lambda > Create Function.
- Select Python Runtime (e.g., Python 3.9).
- Add Dependencies
- Use a Lambda layer or package libraries like
boto3
andslack_sdk
. 3.Write your python code and execute them. (If you want my code , just comment "ease-py-code" on my blog , will share you 🫶 )
Step 4: Add S3 Permissions
Update the Lambda execution role to allow s3:PutObject
, ses:SendEmail
, and ce:GetCostAndUsage
.
Step 5: Test the Lambda
1.Trigger Lambda manually using a test event.
- Verify the cost report is:
- Uploaded to the S3 bucket.
- Emailed via SES.
- Notified in Slack.
Conclusion
With this setup, AWS cost reports are automatically delivered to your inbox and Slack, keeping you updated on spending trends. Fine-tune this solution by customizing the report frequency or grouping costs by other dimensions.
Follow for more and happy learning :)
Top comments (0)