Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


Terraform - Consumption Budget with Action Group for Email Notifications

This runbook provides a breakdown of the Terraform code provided for creating an ARM deployment in Azure. The code creates a resource group, a consumption budget, and an action group to send notifications when the consumption budget threshold is reached.

Terraform Version1.4.5
Azurerm Version3.54.0

Prerequisites

To use this Terraform code, you will need the following:

  • An Azure subscription
  • Have authenticated to Azure. For example, by using the az login command.
  • Terraform installed. See guides Linux Install or Windows Install.

Terraform Block

The terraform block in this Terraform code defines the minimum required version of Terraform and the required providers for the configuration.

terraform {
  required_version = "=1.4.5"
  required_providers {
    azurerm = {
      source  = "azurerm"
      version = "=3.54.0"
    }
  }
}

Variable Block

The variable block in this Terraform code defines a variable that can be used throughout the configuration.

variable "subscription_id" {
  type        = string
  default     = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  description = "Subscription ID that should match the environment"
}

Terraform Provider Block

The provider block in this Terraform code configures the Azure provider, which is responsible for managing resources in the Azure cloud. The azurerm provider is used in this code, and it is defined with the subscription ID variable provided as input.

provider "azurerm" {
  subscription_id = var.subscription_id
  features {
    resource_group {
      prevent_deletion_if_contains_resources = true
    }
  }
}

The subscription_id variable specifies the ID of the Azure subscription where the resources will be provisioned. This variable is defined in the variables section of the code and can be overridden with a value at runtime.

The features block enables specific features of the Azure provider. In this case, the resource_group feature is used to prevent the resource group from being deleted if it contains resources. This is a safety feature to prevent accidental deletion of resources.

Subscription Data

This block retrieves information about the current Azure subscription.

data "azurerm_subscription" "current" {}

Resource Group

The azurerm_resource_group block defines the Azure Resource Group to be created.

resource "azurerm_resource_group" "rg-budgets-001" {
  name     = "rg-budgets-prod-001"
  location = "eastus2"
}
  • name: Specifies the name of the resource group to be created.
  • location: Specifies the Azure region where the resource group should be created.

This block creates an Azure resource group in the eastus2 region with the name rg-budgets-prod-001.

You can modify the name and location values as per your requirements.

Action Group

The azurerm_monitor_action_group block creates an Azure Monitor action group named ag-budgets-prod-001 and associates it with the resource group created earlier. It also specifies an email address to receive notifications.

resource "azurerm_monitor_action_group" "ag-budgets-001" {
  name                = "ag-budgets-prod-001"
  resource_group_name = azurerm_resource_group.rg-budgets-001.name
  short_name          = "ag-bgt-001"

  email_receiver {
    email_address           = "example@email"
        name                = "prod-Budget"
    use_common_alert_schema = true
  }
}
  • name: Specifies the name of the action group to be created.
  • resource_group_name: Specifies the name of the resource group where the action group will be created.
  • short_name: Specifies a short name for the action group.
  • email_receiver: Specifies the email address and name of the person who will receive notifications.
    • email_address: Specifies the email address of the person who will receive notifications.
    • name: Specifies the name of the person who will receive notifications.
    • use_common_alert_schema: Specifies whether the common alert schema should be used.

Consumption Budget

The azurerm_consumption_budget_subscription block creates an Azure consumption budget named bgt-monthly-prod-001 and associates it with the current subscription. It also specifies the budget amount, time grain, time period, and notification settings.

resource "azurerm_consumption_budget_subscription" "bgt-monthly-001" {
  name            = "bgt-monthly-prod-001"
  subscription_id = data.azurerm_subscription.current.id
  amount          = 100
  time_grain      = "Monthly"

  time_period {
    start_date = "2023-04-01T00:00:00Z"
  }

  notification {
    enabled   = true
    threshold = 75.0
    operator  = "EqualTo"

    contact_groups = [
      azurerm_monitor_action_group.ag-budgets-001.id,
    ]
  }
}
  • name: Specifies the name of the consumption budget to be created.
  • subscription_id: Specifies the ID of the subscription to which the consumption budget will be associated.
  • amount: Specifies the budget amount.
  • time_grain: Specifies the time grain for the budget.
  • time_period: Specifies the start and end date of the budget.
    • start_date: Specifies the start date of the budget.
  • notification: Specifies the notification settings for the budget.
    • enabled: Specifies whether notifications are enabled.
    • threshold: Specifies the threshold at which a notification will be sent.
    • operator: Specifies the comparison operator for the threshold.
    • contact_groups: Specifies the action groups to notify when the threshold is reached.

Conclusion

This Terraform code creates an Azure resource group, an action group, and a consumption budget that work together to help manage and monitor Azure consumption costs.

The azurerm_resource_group block creates the resource group that contains the other resources, while the azurerm_monitor_action_group block creates the action group that will receive notifications when the consumption budget reaches its threshold.

Finally, the azurerm_consumption_budget_subscription block creates the consumption budget and specifies the amount, time period, and notification settings. By using this code, you can set up a basic Azure consumption cost management system that can help you avoid unexpected charges and stay within your budget.

The Code

# This block specifies the version of Terraform and the required providers
terraform {
  required_version = "=1.4.5"
  required_providers {
    azurerm = {
      source  = "azurerm"
      version = "=3.54.0"
    }
  }
}

# This block specifies the Azure provider configuration
provider "azurerm" {
  subscription_id = var.subscription_id

  # This block enables a feature that prevents the resource group from being deleted if it contains resources
  features {
    resource_group {
      prevent_deletion_if_contains_resources = true
    }
  }
}

# This block defines a variable that can be used throughout the configuration
variable "subscription_id" {
  type        = string
  default     = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  description = "Subscription ID that should match the environment"
}

# Retrieves information about the current Azure subscription using the azurerm_subscription data source
data "azurerm_subscription" "current" {}

# This block creates an Azure resource group named "rg-budgets-prod-001" in the "eastus2" region
resource "azurerm_resource_group" "rg-budgets-001" {
  name     = "rg-budgets-prod-001"
  location = "eastus2"
}

# This block creates an Azure Monitor action group for budget notifications
resource "azurerm_monitor_action_group" "ag-budgets-001" {
  name                = "ag-budgets-prod-001"
  resource_group_name = azurerm_resource_group.rg-budgets-001.name
  short_name          = "ag-bgt-001"

  # This block configures an email receiver for the action group
  email_receiver {
    email_address           = "example@email.com"
    name                    = "prod-Budget"
    use_common_alert_schema = true
  }
}

# This block creates an Azure consumption budget for the subscription
resource "azurerm_consumption_budget_subscription" "bgt-monthly-001" {
  name            = "bgt-monthly-prod-001"
  subscription_id = data.azurerm_subscription.current.id

  amount     = 100
  time_grain = "Monthly"

  # This block configures a notification for the budget
  notification {
    enabled   = true
    threshold = 75.0
    operator  = "EqualTo"

    # References the ID of the action group created earlier as the contact group for the notification
    contact_groups = [
      azurerm_monitor_action_group.ag-budgets-001.id,
    ]
  }

  # This block configures the start date for the budget period
  time_period {
    start_date = "2023-04-01T00:00:00Z"
  }
}

Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.