Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

Terraform - Deploy and manage Azure action groups

·1170 words·6 mins· 100 views · 5 likes ·
Terraform Microsoft Azure Azure Monitor IaC

Hey, did you know that Azure Monitor has these cool things called action groups? They help you manage alerts by connecting them to different actions. You can receive notifications by email or text message, run Azure Functions, or even start a webhook with an HTTP call.

But perhaps you, like me, do not want to manually deploy and manage these groups of actions as it can become tedious and repetitive.That’s why in this post, I will show you how I use Terraform to automate the deployment and management of our action groups reliably and efficiently.


Prerequisites #

  • You need Terraform CLI on your local machine, if you’re new to using Terraform to deploy Microsoft Azure resources, then I recommend you check out this link.
  • A text editor or IDE of your choice (Visual Studio Code with Terraform extension is my recommendation)
Declare Azure Provider in Terraform>

Declare Azure Provider in Terraform #

The file in Terraform is used to specify and configure the providers used in your Terraform configuration. A provider is a service or platform where the resources will be managed. This could be a cloud provider like Microsoft Azure, AWS, Google Cloud, etc.

This file is important because it tells Terraform which provider’s API to use when creating, updating, and deleting resources. Without it, Terraform wouldn’t know where to manage your resources.

provider "azurerm" {
  features {}
Create Azure Action Groups using Terraform>

Create Azure Action Groups using Terraform #

In the case of Azure Action groups deployment, the file contains the following key components:

  • Resource group: This block creates a resource group in Azure with the given name and location. Resource groups in Azure serve as a logical container for resources deployed on Azure.

  • Action Groups: This block creates multiple Azure Monitor Action Groups using the for_each statement. Each Action Group can be enabled or disabled using the enabled attribute. The name of each Action Group is set to the key of the current object in the action_groups map.

  • Email receiver: In this block, you configure an email recipient for each action group. When an alert is triggered, an email is sent to the specified email_address. The name attribute assigns a name to the recipient of the email.

  • SMS receiver: In this block, an SMS receiver is configured for each Action Group. An SMS is sent to the specified phone number when an alert is triggered. The name attribute assigns a name to the SMS recipient, and country_code specifies the country code of the phone number.

  • Webhook receiver: In this block, a webhook receiver is configured for each Action Group. An HTTP request is made to the specified service_uri when an alert is triggered. The name attribute assigns a name to the receiver of the webhook.

// Resource Group
resource "azurerm_resource_group" "rg" {
  name     =
  location = var.resource_group.location
  tags     = var.tags

// Action Groups
resource "azurerm_monitor_action_group" "ag" {
  for_each            = var.action_groups
  enabled             = each.value.enabled
  name                = each.key
  resource_group_name =
  short_name          = each.value.short_name

  // Email receiver configuration
  email_receiver {
    name          = each.value.email_name
    email_address =

  // SMS receiver configuration
  sms_receiver {
    name         = each.value.sms_name
    country_code =
    phone_number =

  // Webhook receiver configuration
  webhook_receiver {
    name        = each.value.webhook_name
    service_uri = each.value.service_uri

  tags = var.tags
Declaration of input variables>

Declaration of input variables #

The file in Terraform defines the variables I will use in the file. These variables allow for more flexibility and reusability in the code. In this example, the variables are defined in the include:

  • tags: This block declares a variable named tags, which is a map of strings. It is used to assign tags to the Azure resources being created. For example, you can use a key-value pair such as Terraform = true to indicate that the resource was deployed with Terraform.

  • resource_group: In this section, a map variable called resource_group is declared. It should include the name and location keys.

  • action_groups: This block defines a variable called action_groups, a map where each value is an object with properties like short_name, email_name, email, sms_name, country, phone, webhook_name, and service_uri.

// Variable for Azure Resource Group settings
variable "resource_group" {
  description = "Azure Resource Group settings"
  type = object({
    name     = string
    location = string

// Variable for tags
variable "tags" {
  description = "Common tags for all resources"
  type = object({
    Environment = string
    Terraform   = string
  default = {
    Environment = ""
    Terraform   = "true"

// Variable for actions groups settings
variable "action_groups" {
  description = "Map of action groups"
  type = map(object({
    enabled      = bool
    short_name   = string
    email_name   = string
    email        = string
    sms_name     = string
    country      = string
    phone        = string
    webhook_name = string
    service_uri  = string
Declaration of output values>

Declaration of output values #

The file in Terraform extracts and displays information about the resources created or managed by your Terraform configuration. These outputs are defined using the output keyword and can be used to return information that can be useful for the user, for other Terraform configurations, or for programmatically using the information in scripts or other tools.

In this example, the file returns information about the resource group and each Azure Monitor Action Group created.

Once Terraform has finished applying your configuration, it will display the defined outputs.

// Output for the created resource group
output "resource_group" {
  description = "The complete resource group object"
  value       = azurerm_resource_group.rg

// Output for the created actions groups
output "action_groups" {
  description = "The complete action group objects"
  value       =
Executing the Terraform Deployment>

Executing the Terraform Deployment #

Now that you’ve declared the resources correctly, it’s time to take the following steps to deploy them in your Azure environment.

  • Initialization: To begin, execute the terraform init command. This will initialize your working directory that holds the .tf files and download the provider specified in the file, and configure the Terraform backend. I suggest looking at this link if you’re curious about the process.

  • Planning: Next, execute the terraform plan. This command creates an execution plan and shows Terraform’s actions to achieve the desired state defined in your .tf files. This gives you a chance to review the changes before applying them.

  • Apply: When you’re satisfied with the plan, execute the terraform apply command. This will implement the required modifications to attain the intended infrastructure state. Before making any changes, you will be asked to confirm your decision.

  • Inspection: After applying the changes, you can use terraform show command to see the current state of your infrastructure.

  • Destroy (optional): when a project is no longer needed or when resources have become outdated. You can use the terraform destroy command. This will remove all the resources that Terraform has created.

References and useful links #

Thank you for taking the time to read my post. I sincerely hope that you find it helpful.