Slack Integration for Budget Alerts
Prerequisites
- AWS account with appropriate permissions
- Terraform >= 1.0
- Slack workspace with incoming webhook configured
Step 1: Create the Lambda Function for Slack
module "slack_notifier" {
source = "registry.patterneddesigns.ca/patterneddesigns/lambda-function/aws"
version = "3.1.0"
function_name = "budget-slack-notifier"
runtime = "python3.12"
handler = "main.handler"
source_path = "./src/slack-notifier"
environment_variables = {
SLACK_WEBHOOK_URL = var.slack_webhook_url
}
}
Step 2: Create Budget with SNS Integration
resource "aws_sns_topic" "budget_alerts" {
name = "budget-alerts"
}
resource "aws_sns_topic_policy" "budget_alerts" {
arn = aws_sns_topic.budget_alerts.arn
policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = { Service = "budgets.amazonaws.com" }
Action = "SNS:Publish"
Resource = aws_sns_topic.budget_alerts.arn
}]
})
}
module "budget" {
source = "registry.patterneddesigns.ca/governance/cost-controls/aws"
version = "1.2.0"
budget_name = "production-budget"
budget_limit = 10000
sns_topic_arn = aws_sns_topic.budget_alerts.arn
alert_thresholds = [50, 75, 90, 100, 110]
notification_emails = ["finance@example.com"]
}
Step 3: Connect Lambda to SNS
resource "aws_lambda_permission" "sns" {
statement_id = "AllowSNSInvoke"
action = "lambda:InvokeFunction"
function_name = module.slack_notifier.function_name
principal = "sns.amazonaws.com"
source_arn = aws_sns_topic.budget_alerts.arn
}
resource "aws_sns_topic_subscription" "lambda" {
topic_arn = aws_sns_topic.budget_alerts.arn
protocol = "lambda"
endpoint = module.slack_notifier.function_arn
}
Step 4: Deploy and Test
Run terraform apply to deploy the complete solution.
Budget alerts will now be sent to both email recipients and your Slack channel.