Ends in
00
days
00
hrs
00
mins
00
secs
ENROLL NOW

Get any AWS Specialty Mock Test for FREE when you Buy 2 AWS Pro-Level Practice Tests – as LOW as $10.49 USD each ONLY!

Setting Up AWS Billing Alerts on Slack

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

Tutorials dojo strip

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.

Get any AWS Specialty Mock Test for FREE when you Buy 2 AWS Pro-Level Practice Tests – as LOW as $10.49 USD each ONLY!

Tutorials Dojo portal

Learn AWS with our PlayCloud Hands-On Labs

Tutorials Dojo Exam Study Guide eBooks

tutorials dojo study guide eBook

FREE AWS Exam Readiness Digital Courses

Subscribe to our YouTube Channel

Tutorials Dojo YouTube Channel

FREE AWS, Azure, GCP Practice Test Samplers

Follow Us On Linkedin

Recent Posts

Written by: Carlo Acebedo

Carlo is a cloud engineer and a content creator at Tutorials Dojo. He's also a member of the AWS Community builder and holds 5 AWS Certifications. Carlo specializes in building and automating solutions in the Amazon Web Services Cloud.

AWS, Azure, and GCP Certifications are consistently among the top-paying IT certifications in the world, considering that most companies have now shifted to the cloud. Earn over $150,000 per year with an AWS, Azure, or GCP certification!

Follow us on LinkedIn, YouTube, Facebook, or join our Slack study group. More importantly, answer as many practice exams as you can to help increase your chances of passing your certification exams on your first try!

View Our AWS, Azure, and GCP Exam Reviewers Check out our FREE courses

Our Community

~98%
passing rate
Around 95-98% of our students pass the AWS Certification exams after training with our courses.
200k+
students
Over 200k enrollees choose Tutorials Dojo in preparing for their AWS Certification exams.
~4.8
ratings
Our courses are highly rated by our enrollees from all over the world.

What our students say about us?