Introduction
Let’s first get familiar with the AWS Cost Explorer. It’s essentially a dedicated financial advisor for your AWS expenses, helping you make sense of where your spending goes. With an easy-to-use interface, AWS Cost Explorer provides detailed visual analytics of your past, present, and predicted AWS usage and costs.
Why Slack for AWS bill?
We’ve all been there, trying to stay on top of our monthly bills. AWS does provide budget alerts, which are pretty handy in preventing spending overruns. But those alerts only chime in when you hit certain thresholds. If you want to see how your cost is inching up, you’d still have to hop into the AWS Cost Explorer console. That’s a lot of toggling back and forth, isn’t it?
Now, imagine if you could bring the data from AWS Cost Explorer right into Slack or any communication platform you’re using. You could receive real-time updates on your AWS costs right in the midst of your team’s everyday conversation. No need to jump to another tab or remind yourself to check costs; the information would simply be there in your Slack channel, updated and ready for review.
In this article, I will show you how to integrate AWS Cost Explorer data into Slack by providing a Python script that you can easily plug into a Lambda function.
Code explanation
This script leverages Boto3 to fetch cost data from AWS Cost Explorer. It also uses the urllib3 library to send HTTP requests, specifically to post messages to your Slack channel.
The script has several important parts:
Import statements and global variables
The script starts by importing several necessary Python libraries. The Boto3 library for AWS interaction, date-time libraries for formatting and manipulating dates, the JSON library for handling JSON data, and urllib3 for HTTP requests. It also creates a Cost Explorer client using Boto3 and an HTTP pool manager using urllib3. Additionally, a global variable called hook
is defined to hold the URL for a Slack webhook, which is used to post messages to Slack.
Cost retrieval functions
The script includes two functions for retrieving cost information from AWS Cost Explorer:
- get_current_month_cost
- get_forecasted_month_cost.
The get_current_month_cost function fetches the current cost of AWS usage for the month so far, while get_forecasted_month_cost retrieves a cost forecast for the rest of the month. Both functions specifically focus on the ‘UnblendedCost’ metric. In AWS billing terms, ‘UnblendedCost’ represents the raw, unaggregated cost of each service. It doesn’t apply any discounts from Reserved Instances, Savings Plans, or volume discount rates. This can be especially useful for a more granular understanding of how each AWS service contributes to your total costs.
Additionally, both functions filter out ‘Credit’ record types. Credits in AWS are typically promotional credits or refunds given by AWS, and by filtering them out, the script focuses solely on the actual costs incurred. This provides a clearer picture of your real expenditure without the noise of credit entries, allowing for more accurate cost analysis and forecasting.
Date helper functions
get_first_day_of_month and get_last_day_of_month are two helper functions for manipulating dates. They take a date as input and return a string representing the first and last day of the respective month. These functions assist in defining the time periods for retrieving AWS cost data.
Main function (lambda_handler)
This function is the core of the script, which AWS Lambda invokes when triggered. The function starts by retrieving the current date and then using it to calculate the current and forecasted AWS cost for the month. It constructs a message detailing these costs and posts this message to Slack using the webhook URL.
Conclusion
And there you have it! By implementing this Python script in an AWS Lambda function, you’ve just made your AWS cost monitoring a whole lot simpler. As a next step, consider configuring EventBridge schedules to automatically invoke the Lambda function. That way, you can receive daily updates making sure you’re always informed about your AWS costs without even lifting a finger.