Skip to content

Commit ae367aa

Browse files
committed
DTOSS-8458: Create a Terraform module for Azure Action Group
1 parent 30decb1 commit ae367aa

File tree

5 files changed

+131
-98
lines changed

5 files changed

+131
-98
lines changed
Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
11
module "monitor_action_group" {
2-
source = "./modules/monitor-action-group"
2+
for_each = local.monitor_action_group_map
33

4-
name = "example-monitor-action-group"
5-
resource_group_name = azurerm_resource_group.core[each.key].name
6-
location = each.key
7-
short_name = "testing123"
4+
source = "../monitor-action-group"
85

9-
email_receiver = [
10-
{
11-
name = "testing"
12-
email_address = "testing@testing.com"
13-
use_common_alert_schema = false
14-
}
15-
]
6+
name = "${module.regions_config[each.value.region].names.monitor-action-group}-${lower(each.value.short_name)}"
7+
resource_group_name = azurerm_resource_group.core[each.value.region].name
8+
location = each.value.region
9+
short_name = each.value.short_name
10+
email_receiver = each.value.email_receiver
11+
event_hub_receiver = each.value.event_hub_receiver
12+
sms_receiver = each.value.sms_receiver
13+
voice_receiver = each.value.voice_receiver
14+
webhook_receiver = each.value.webhook_receiver
15+
}
16+
17+
locals {
18+
monitor_action_group_object_list = flatten([
19+
for region in keys(var.regions) : [
20+
for action_group_key, action_group_details in var.monitor_action_group : merge(
21+
{
22+
region = region
23+
action_group_key = action_group_key
24+
},
25+
action_group_details
26+
)
27+
]
28+
])
29+
30+
# ...then project the list of objects into a map with unique keys (combining the iterators), for consumption by a for_each meta argument
31+
monitor_action_group_map = {
32+
for object in local.monitor_action_group_object_list : "${object.action_group_key}-${object.region}" => object
33+
}
1634
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
variable "monitor_action_group" {
3+
description = "Default configuration for the monitor action groups."
4+
type = map(object({
5+
short_name = string
6+
email_receiver = optional(map(object({
7+
name = string
8+
email_address = string
9+
use_common_alert_schema = optional(bool, false)
10+
})))
11+
event_hub_receiver = optional(map(object({
12+
name = string
13+
event_hub_namespace = string
14+
event_hub_name = string
15+
subscription_id = string
16+
use_common_alert_schema = optional(bool, false)
17+
})))
18+
sms_receiver = optional(map(object({
19+
name = string
20+
country_code = string
21+
phone_number = string
22+
})))
23+
voice_receiver = optional(map(object({
24+
name = string
25+
country_code = string
26+
phone_number = string
27+
})))
28+
webhook_receiver = optional(map(object({
29+
name = string
30+
service_uri = string
31+
use_common_alert_schema = optional(bool, false)
32+
})))
33+
34+
}))
35+
}

infrastructure/modules/monitor-action-group/main.tf

Lines changed: 28 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,48 @@
1-
# resource "azurerm_log_analytics_workspace" "this" {
2-
# name = "workspace-01"
3-
# location = azurerm_resource_group.example.location
4-
# resource_group_name = azurerm_resource_group.example.name
5-
# }
6-
71
resource "azurerm_monitor_action_group" "this" {
82
name = var.name
93
resource_group_name = var.resource_group_name
104
short_name = var.short_name
115

12-
# arm_role_receiver {
13-
# name = "armroleaction"
14-
# role_id = "de139f84-1756-47ae-9be6-808fbbe84772"
15-
# use_common_alert_schema = true
16-
# }
17-
18-
# automation_runbook_receiver {
19-
# name = "action_name_1"
20-
# automation_account_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-runbooks/providers/Microsoft.Automation/automationAccounts/aaa001"
21-
# runbook_name = "my runbook"
22-
# webhook_resource_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-runbooks/providers/Microsoft.Automation/automationAccounts/aaa001/webHooks/webhook_alert"
23-
# is_global_runbook = true
24-
# service_uri = "https://s13events.azure-automation.net/webhooks?token=randomtoken"
25-
# use_common_alert_schema = true
26-
# }
27-
28-
# azure_app_push_receiver {
29-
# name = "pushtoadmin"
30-
# email_address = "admin@contoso.com"
31-
# }
32-
33-
# azure_function_receiver {
34-
# name = "funcaction"
35-
# function_app_resource_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-funcapp/providers/Microsoft.Web/sites/funcapp"
36-
# function_name = "myfunc"
37-
# http_trigger_url = "https://example.com/trigger"
38-
# use_common_alert_schema = true
39-
# }
40-
416
dynamic "email_receiver" {
42-
for_each = toset(var.email_receiver)
7+
for_each = var.email_receiver != null ? var.email_receiver : {}
438
content {
449
name = email_receiver.value.name
4510
email_address = email_receiver.value.email_address
4611
use_common_alert_schema = email_receiver.value.use_common_alert_schema
4712
}
4813
}
4914

50-
# email_receiver {
51-
# name = "sendtoadmin"
52-
# email_address = "admin@contoso.com"
53-
# }
54-
55-
# email_receiver {
56-
# name = "sendtodevops"
57-
# email_address = "devops@contoso.com"
58-
# use_common_alert_schema = true
59-
# }
60-
61-
# event_hub_receiver {
62-
# name = "sendtoeventhub"
63-
# event_hub_namespace = "eventhubnamespace"
64-
# event_hub_name = "eventhub1"
65-
# subscription_id = "00000000-0000-0000-0000-000000000000"
66-
# use_common_alert_schema = false
67-
# }
68-
69-
# itsm_receiver {
70-
# name = "createorupdateticket"
71-
# workspace_id = "${data.azurerm_client_config.current.subscription_id}|${azurerm_log_analytics_workspace.example.workspace_id}"
72-
# connection_id = "53de6956-42b4-41ba-be3c-b154cdf17b13"
73-
# ticket_configuration = "{\"PayloadRevision\":0,\"WorkItemType\":\"Incident\",\"UseTemplate\":false,\"WorkItemData\":\"{}\",\"CreateOneWIPerCI\":false}"
74-
# region = "southcentralus"
75-
# }
76-
77-
# logic_app_receiver {
78-
# name = "logicappaction"
79-
# resource_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-logicapp/providers/Microsoft.Logic/workflows/logicapp"
80-
# callback_url = "https://logicapptriggerurl/..."
81-
# use_common_alert_schema = true
82-
# }
15+
dynamic "event_hub_receiver" {
16+
for_each = var.event_hub_receiver != null ? var.event_hub_receiver : {}
17+
content {
18+
name = event_hub_receiver.value.name
19+
event_hub_namespace = event_hub_receiver.value.event_hub_namespace
20+
event_hub_name = event_hub_receiver.value.event_hub_name
21+
subscription_id = event_hub_receiver.value.subscription_id
22+
use_common_alert_schema = event_hub_receiver.value.use_common_alert_schema
23+
}
24+
}
8325

84-
# sms_receiver {
85-
# name = "oncallmsg"
86-
# country_code = "1"
87-
# phone_number = "1231231234"
88-
# }
26+
dynamic "sms_receiver" {
27+
for_each = var.sms_receiver != null ? var.sms_receiver : {}
28+
content {
29+
name = sms_receiver.value.name
30+
country_code = sms_receiver.value.country_code
31+
phone_number = sms_receiver.value.phone_number
32+
}
33+
}
8934

90-
# voice_receiver {
91-
# name = "remotesupport"
92-
# country_code = "86"
93-
# phone_number = "13888888888"
94-
# }
35+
dynamic "voice_receiver" {
36+
for_each = var.voice_receiver != null ? var.voice_receiver : {}
37+
content {
38+
name = voice_receiver.value.name
39+
country_code = voice_receiver.value.country_code
40+
phone_number = voice_receiver.value.phone_number
41+
}
42+
}
9543

9644
dynamic "webhook_receiver" {
97-
for_each = toset(var.webhook_receiver)
45+
for_each = var.webhook_receiver != null ? var.webhook_receiver : {}
9846
content {
9947
name = webhook_receiver.value.name
10048
service_uri = webhook_receiver.value.service_uri

infrastructure/modules/monitor-action-group/variables.tf

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,55 @@ variable "short_name" {
1414
}
1515

1616
variable "email_receiver" {
17-
description = "email receiver properties."
18-
type = list(object({
17+
description = "Email receiver properties."
18+
type = map(object({
1919
name = string
2020
email_address = string
21+
use_common_alert_schema = optional(bool, false)
22+
}))
23+
default = {}
24+
}
25+
26+
variable "event_hub_receiver" {
27+
description = "event hub receiver properties."
28+
type = map(object({
29+
name = string
30+
event_hub_namespace = string
31+
event_hub_name = string
32+
subscription_id = string
2133
use_common_alert_schema = bool
2234
}))
23-
default = []
35+
default = {}
36+
}
37+
38+
variable "sms_receiver" {
39+
description = "sms receiver properties."
40+
type = map(object({
41+
name = string
42+
country_code = string
43+
phone_number = string
44+
}))
45+
default = {}
46+
}
47+
48+
variable "voice_receiver" {
49+
description = "voice receiver properties."
50+
type = map(object({
51+
name = string
52+
country_code = string
53+
phone_number = string
54+
}))
55+
default = {}
2456
}
2557

2658
variable "webhook_receiver" {
2759
description = "webhook receiver properties."
28-
type = list(object({
60+
type = map(object({
2961
name = string
3062
service_uri = string
3163
use_common_alert_schema = bool
3264
}))
33-
default = []
65+
default = {}
3466
}
3567

3668
variable "location" {

infrastructure/modules/shared-config/output.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ locals {
121121
logic-app = lower("LA-${var.env}-${var.location_map[var.location]}-${var.application}")
122122
managed-devops-pool = lower("private-pool-${var.env}-${var.location_map[var.location]}")
123123
managed-identity = lower("MI-${var.env}-${var.location_map[var.location]}-${var.application}")
124-
monitor_action_group_name = lower("AG-${var.env}-${var.location_map[var.location]}-${var.application}")
124+
monitor-action-group = lower("AG-${var.env}-${var.location_map[var.location]}-${var.application}")
125125
network-interface = upper("${var.env}-${var.location_map[var.location]}-${var.application}")
126126
network-security-group = upper("NSG-${var.env}-${var.location_map[var.location]}-${var.application}")
127127
postgres-sql-server = lower("postgres-${var.application}-${var.env}-${var.location_map[var.location]}")

0 commit comments

Comments
 (0)