Using Azure Action Groups with Code

When creating email notifications from Azure infrastructure, Action Groups are a key resource. Some monitoring features, such as Budgets, allows you to add email recipients directly, but these are limited (in the case of budgets to 5 recipient addresses) and if a new admin needs adding later they would need adding to every single target, whereas an Action Group would mean this change is in just one place.

Additionally action groups can notify via the Azure mobile app, SMS, or even voice rather than just email.

Below are a couple of examples, using Terraform and Azure PowerShell, of how to create an Action Group using code. Both take a variable containing a set of email addresses and names and apply that to a new Action Group resource.

Terraform

To configure in Terraform looks like this, we have a variable containing a list of names and email addresses as a map. This is then used by a Dynamic Block loop to add multiple email receivers to the Action Group.

 1variable "action_group_email_list" {
 2  type = list(map(string))
 3  default = [
 4    { name = "Admin Person 1", email = "admin-one@example.com" },
 5    { name = "Admin Person 2", email = "admin-two@example.com" },
 6    #....
 7    { name = "Admin Person n", email = "admin-n@example.com" },
 8  ]
 9  description = "A list of names and email addresses to be added to the Action Group list"
10}
11
12resource "azurerm_monitor_action_group" "ag" {
13  name                = "example-actiongroup"
14  resource_group_name = azurerm_resource_group.rg.name
15  short_name          = "Email Admins"
16  #Populate email_receiver (s) with var.action_group_email_list
17  # using a Dynamic Block to loop through list
18  dynamic "email_receiver" {
19    for_each = var.action_group_email_list
20    content {
21      name          = email_receiver.value["name"]
22      email_address = email_receiver.value["email"]
23    }
24  }
25}
26
27#Resource Group to hold the Action Group
28resource "azurerm_resource_group" "rg" {
29  name     = "example-rsg"
30  location = "eastus"
31}
32
33# Configure the Azure provider
34terraform {
35  required_providers {
36    azurerm = {
37      source  = "hashicorp/azurerm"
38      version = ">= 3.7.0, < 4.0.0"
39    }
40  }
41  required_version = ">= 1.6.0"
42
43}
44provider "azurerm" {
45  skip_provider_registration = "true"
46  features {}
47}

PowerShell

Alternatively in PowerShell we can do this as follows:

 1#Define a list of email recipients
 2$action_group_email_list= [PSCustomObject]@{EmailAddress= "admin-one@example.com"; Name= "Admin Person 1"},
 3    [PSCustomObject]@{EmailAddress= "admin-two@example.com"; Name= "Admin Person 2"},
 4    [PSCustomObject]@{EmailAddress= "admin-n@example.com"; Name= "Admin Person n"}
 5#Convert the list into ActionGroupEmailReceiverObjects
 6$EmailReceivers= Foreach ($recipient in $action_group_email_list) {
 7    New-AzActionGroupEmailReceiverObject -emailaddress $recipient.EmailAddress `
 8      -Name $recipient.Name
 9    }
10#Create the Action Group
11New-AzActionGroup -Name "example-actiongroup" -ResourceGroupName "example-rsg" `
12  -Location "Global" -EmailReceiver $EmailReceivers -GroupShortName "Email admins"