These step-by-step demonstrations walk you through complete workflows using the cost-controls module. Each demonstration includes prerequisites, detailed instructions, and verification steps.

Getting Started

To follow any demonstration, ensure you have:

  1. Prerequisites met: Terraform >= 1.0, AWS CLI configured
  2. Authenticate with the registry: terraform login registry.patterneddesigns.ca
  3. Clone the demonstration repository: git clone <demo-repo-url>
  4. Follow the step-by-step instructions below

Step-by-Step Guides

Setting Up Budget Alerts

Deploy a complete budget with multi-threshold alerts and SNS integration

Prerequisites

  • AWS account with appropriate permissions
  • Terraform >= 1.0
  • Email addresses for notifications

Step 1: Create the Budget

module "production_budget" {
  source  = "registry.patterneddesigns.ca/governance/cost-controls/aws"
  version = "1.2.0"

  budget_name  = "production-monthly-budget"
  budget_limit = 5000

  alert_thresholds = [50, 75, 90, 100]

  notification_emails = [
    "finance@example.com",
    "cloud-ops@example.com"
  ]
}

Step 2: Add SNS Integration

Configure an SNS topic for additional notification channels.

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 "production_budget" {
  source  = "registry.patterneddesigns.ca/governance/cost-controls/aws"
  version = "1.2.0"

  budget_name   = "production-monthly-budget"
  budget_limit  = 5000
  sns_topic_arn = aws_sns_topic.budget_alerts.arn

  alert_thresholds    = [50, 75, 90, 100]
  notification_emails = ["finance@example.com"]
}

Step 3: Deploy and Verify

Run terraform apply and verify the budget in the AWS Console.

terraform init
terraform plan
terraform apply

Step 4: Test Alerts

Use the AWS CLI to verify the budget was created:

aws budgets describe-budgets --account-id $(aws sts get-caller-identity --query Account --output text)

Slack Integration for Budget Alerts

Send budget alerts to Slack channels using SNS and Lambda

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.