These examples demonstrate practical, real-world usage patterns for the cost-controls module. Each example is self-contained and ready to run—simply copy the configuration, customize the values for your environment, and apply.

Getting Started

To run any example, follow these steps:

  1. Authenticate with the registry: terraform login registry.patterneddesigns.ca
  2. Initialize the working directory: terraform init
  3. Review the execution plan: terraform plan
  4. Apply the configuration: terraform apply

Usage Examples

Basic Usage

Minimal configuration for a monthly budget with email alerts

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

  budget_name  = "monthly-budget"
  budget_limit = 1000

  notification_emails = ["finance@example.com"]
}

Monthly Budget with Multi-Threshold Alerts

Monthly budget with multiple alert thresholds for gradual notifications

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

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

  # Alert at 25%, 50%, 75%, 90%, and 100% of budget
  alert_thresholds = [25, 50, 75, 90, 100]

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

Service-Specific Budget

Budget filtered to a specific AWS service

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

  budget_name  = "ec2-compute-budget"
  budget_limit = 5000

  alert_thresholds = [75, 90, 100]

  notification_emails = ["infrastructure@example.com"]

  cost_filters = {
    Service = ["Amazon Elastic Compute Cloud - Compute"]
  }
}

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

  budget_name  = "rds-database-budget"
  budget_limit = 3000

  alert_thresholds = [75, 90, 100]

  notification_emails = ["database-team@example.com"]

  cost_filters = {
    Service = ["Amazon Relational Database Service"]
  }
}

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

  budget_name  = "s3-storage-budget"
  budget_limit = 1000

  alert_thresholds = [80, 100]

  notification_emails = ["storage-team@example.com"]

  cost_filters = {
    Service = ["Amazon Simple Storage Service"]
  }
}

Budget with SNS Integration

Budget with SNS topic for custom notification workflows

# Create SNS topic for budget alerts
resource "aws_sns_topic" "budget_alerts" {
  name = "budget-alerts"
}

# Allow AWS Budgets to publish to the topic
resource "aws_sns_topic_policy" "budget_alerts" {
  arn = aws_sns_topic.budget_alerts.arn

  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Sid       = "AllowBudgetsPublish"
      Effect    = "Allow"
      Principal = { Service = "budgets.amazonaws.com" }
      Action    = "SNS:Publish"
      Resource  = aws_sns_topic.budget_alerts.arn
    }]
  })
}

# Create budget with SNS integration
module "budget" {
  source  = "registry.patterneddesigns.ca/governance/cost-controls/aws"
  version = "1.2.0"

  budget_name  = "production-budget"
  budget_limit = 15000

  alert_thresholds = [50, 75, 90, 100, 110]

  notification_emails = ["finance@example.com"]
  sns_topic_arn       = aws_sns_topic.budget_alerts.arn
}

# Subscribe Lambda function for automated actions
resource "aws_sns_topic_subscription" "lambda" {
  topic_arn = aws_sns_topic.budget_alerts.arn
  protocol  = "lambda"
  endpoint  = aws_lambda_function.cost_handler.arn
}

# Subscribe Slack webhook
resource "aws_sns_topic_subscription" "slack" {
  topic_arn = aws_sns_topic.budget_alerts.arn
  protocol  = "https"
  endpoint  = var.slack_webhook_url
}

Multi-Environment Budgets

Separate budgets for different environments with varying limits

locals {
  environments = {
    production = {
      budget_limit     = 20000
      alert_thresholds = [50, 75, 90, 95, 100, 110]
      emails           = ["finance@example.com", "cto@example.com", "prod-ops@example.com"]
    }
    staging = {
      budget_limit     = 3000
      alert_thresholds = [75, 100]
      emails           = ["dev-ops@example.com"]
    }
    development = {
      budget_limit     = 1000
      alert_thresholds = [80, 100, 150]
      emails           = ["dev-team@example.com"]
    }
  }
}

module "env_budgets" {
  for_each = local.environments

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

  budget_name  = "${each.key}-monthly-budget"
  budget_limit = each.value.budget_limit

  alert_thresholds    = each.value.alert_thresholds
  notification_emails = each.value.emails

  cost_filters = {
    TagKeyValue = ["Environment$${each.key}"]
  }
}

# Outputs for each environment budget
output "budget_arns" {
  description = "ARNs of all environment budgets"
  value       = { for k, v in module.env_budgets : k => v.budget_arn }
}