IaC Cost Patterns

A collection of patterns and antipatterns for managing cost in IaC files.

Pattern: Budget

Use budgets to receive alerts about charged and forecasted costs and control spending.

Context

The lack of explicit cost monitoring can often lead to unforeseen and undesirable costs.

Solution

Major cloud providers support the creation of budgets, which allow users to define alerts about charged and forecasted costs and control spending. Having one or more budgets can help monitor and manage the cost of cloud deployments.

Example

Define a budget for a cost limit of 1200 USD for EC2, and generate an email notification if the forecasted monthly cost exceeds this amount:

resource "aws_budgets_budget" "example" {
  name              = "example"
  budget_type       = "COST"
  limit_amount      = "1200"
  limit_unit        = "USD"
  time_unit         = "MONTHLY"

  cost_filter {
    name = "Service"
    values = [
      "Amazon Elastic Compute Cloud - Compute",
    ]
  }

  notification {
    comparison_operator        = "GREATER_THAN"
    threshold                  = 100
    threshold_type             = "PERCENTAGE"
    notification_type          = "FORECASTED"
    subscriber_email_addresses = ["test@example.com"]
  }
}

References

Occurrences

Terraform

AWS CloudFormation