Last updated on June 21, 2024
Introduction
Managing cloud costs effectively is crucial for businesses. AWS provides a feature to set budget alerts that notify you when your costs exceed a certain threshold. While these alerts can be sent via email, they can sometimes get lost in the flurry of daily emails, leading to delayed responses.
This is where real-time Slack notifications for AWS budget alerts come into play. By integrating AWS budget alerts with Slack, these notifications are sent directly to your Slack workspace. This ensures that the alerts are seen and acted upon in a timely manner, helping to prevent unexpected costs.
This blog post will guide you through the steps to set up Slack notifications for AWS budget alerts. This setup will allow you to receive notifications in your Slack workspace when your AWS costs exceed a certain threshold.
Implementation Steps:
Step 1: Login to your AWS Account.
Step 2: Create a Lambda Function
First, we need to create a Lambda function that will send the notifications to Slack. Here are the details for the function:
- Function name:
budget-alert-notifier
- Runtime: Python 3.12
Step 3: Lambda Function Code
Next, we need to add the code for our Lambda function. Here’s the Python code for the function. Feel free to modify this code based on your requirements.:
Step 4: Edit the General Configuration
In the General Configuration settings of your Lambda function, set the Timeout to 1 minute.
Step 5: Set Permissions
Under the Configuration of your Lambda function, go to the Permissions tab, then click on the existing execution role under the “Execution role” panel. This will take you to the IAM console, where you can add the necessary permissions by creating an inline policy. The permissions to add are GetCostAndUsage
and SNSPublish
.
GetCostAndUsage
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": "ce:GetCostAndUsage", "Resource": "*" } ] }
SNSPublish
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": "sns:Publish", "Resource": "*" } ] }
-
GetCostAndUsage: This permission is required to allow the Lambda function to retrieve cost and usage data from AWS. The
ce:GetCostAndUsage
action allows the function to access the AWS Cost Explorer Service, which provides cost and usage data that is used to check if the costs exceed a certain limit. -
SNSPublish: This permission is needed to allow the Lambda function to send messages to the Amazon SNS Topic. The
sns:Publish
action enables the function to publish the prepared message (the alert) to the specified Amazon SNS topic.
Step 6: Trigger the Lambda Function using Amazon SNS
Create an Amazon SNS Topic named budget-alert
and modify the Access Policy.
Type: Standard
Click the Create Topic button at the bottom.
After that, copy the ARN of your SNS topic and modify the access policy. Refer to the JSON policy below.
{ "Version": "2008-10-17", "Id": "__default_policy_ID", "Statement": [ { "Sid": "__default_statement_ID", "Effect": "Allow", "Principal": { "AWS": "*" }, "Action": [ "SNS:GetTopicAttributes", "SNS:SetTopicAttributes", "SNS:AddPermission", "SNS:RemovePermission", "SNS:DeleteTopic", "SNS:Subscribe", "SNS:ListSubscriptionsByTopic", "SNS:Publish", "SNS:Receive" ], "Resource": "your topic ARN", "Condition": { "StringEquals": { "AWS:SourceOwner": "<account-id>" } } }, { "Sid": "Budget_Alert", "Effect": "Allow", "Principal": { "Service": "budgets.amazonaws.com" }, "Action": "SNS:Publish", "Resource": "your topic ARN" } ] }
Step 7: Add the SNS Topic to the Lambda Function
Go back to the AWS Lambda Function, and under Configuration, go to the Triggers tab and add the created SNS topic.
Step 8: Configure AWS Budgets
Go to AWS Budgets and configure the alerts.
Step 9: Add the Amazon SNS Alerts
Add the ARN of the created SNS topic to the Amazon SNS Alerts field. Once done, click next, then save the changes.
And that’s it! You’ve now set up real-time Slack notifications for AWS budget alerts. This setup not only helps in efficient cost management by keeping you informed about your AWS expenses in real-time but also leverages the convenience and immediacy of Slack, making it a valuable tool for any organization using AWS and Slack.