-
Notifications
You must be signed in to change notification settings - Fork 705
Implement Related Resources
Continuing with the example of consumption budgets, the Terraform registry shows that it has an attribute contact_groups
that can be integrated with Action Group IDs. To achieve this, the monitor_action_group
resource first has to be implemented and then integrated with consumption budgets
The steps to integrate are:
The strategies to implement this is similar to how the consumption budget was implemented.
The important part needed for integration is the monitor_action_group
id. First export it at the resource level
modules/monitoring/monitor_action_group/output.tf
output "id" {
description = "The ID of the Action Group"
value = azurerm_monitor_action_group.this.id
}
Then export it at the module level
module "monitor_action_groups" {
source = "./modules/monitoring/monitor_action_group"
for_each = local.shared_services.monitor_action_groups
global_settings = local.global_settings
resource_group_name = local.combined_objects_resource_groups[try(each.value.lz_key, local.client_config.landingzone_key)][each.value.resource_group_key].name
settings = each.value
}
output "monitor_action_groups" {
value = module.monitor_action_groups
}
Once exported, integrate it with the locals.combined_objects.tf
file.
locals {
combined_objects_monitor_action_groups = merge(tomap({ (local.client_config.landingzone_key) = module.monitor_action_groups }), try(var.remote_objects.monitor_action_groups, {}))
}
examples
Start with the examples, creating a new examples file that integrates the monitor_action_group
with consumption_budgets_resource_group
examples/consumption_budget/102-consumption-budget-rg-alerts/configuration.tfvars
global_settings = {
default_region = "region1"
regions = {
region1 = "southeastasia"
}
random_length = 5
}
resource_groups = {
test = {
name = "test"
}
}
monitor_action_groups = {
resource_group_alerts = {
action_group_name = "ag_resourcegroup"
resource_group_key = "test"
shortname = "rgAlerts"
}
}
consumption_budgets = {
test_budget = {
resource_group = {
key = "test"
}
name = "example"
amount = 1000
time_grain = "Monthly"
time_period = {
# uncomment to customize start_date
# start_date = "2022-06-01T00:00:00Z"
}
notifications = {
contact_group = {
enabled = true
threshold = 85.0
operator = "EqualTo"
# lz_key = "examples"
contact_groups_keys = [
"resource_group_alerts",
]
}
}
filter = {
dimensions = {
explicit_name = {
name = "ResourceGroupName"
operator = "In"
values = [
"example",
]
},
resource_group_key = {
# lz_key = "examples"
name = "resource_group_key"
values = [
"test",
]
}
}
}
}
}
- Initialize a
monitor_action_groups
that has an object with the keyresource_group_alerts
- Reference the
resource_group_alerts
key incontact_group
within thenotifications
block
root
Back at the root level, in the previous section, the monitor_action_group
id was exported as a monitor_action_groups
object. This in turn was integrated into the local combined_objects
. In consumption_budgets.tf
, reference the monitor_action_group
from locals.combined_objects_monitor_action_groups
as shown below:
consumption_budgets.tf
module "consumption_budgets_resource_groups" {
source = "./modules/consumption_budget/resource_group"
# truncated
local_combined_resources = {
monitor_action_groups = local.combined_objects_monitor_action_groups,
}
# truncated
client_config = local.client_config
settings = each.value
}
- The
id
will be retrieved frommonitor_action_groups
by referencing the object's key and extracting theid
from the nested object
modules/consumption_budget/resource_group/
With this new variable, add it to variables.tf
variable "local_combined_resources" {
description = "object of local combined resources"
}
The logic is handled in resource_group_budget.tf
resource "azurerm_consumption_budget_resource_group" "this" {
dynamic "notification" {
for_each = var.settings.notifications
content {
operator = notification.value.operator
threshold = notification.value.threshold
contact_emails = try(notification.value.contact_emails, [])
contact_groups = try(notification.value.contact_groups, try(flatten([
for key, value in var.local_combined_resources["monitor_action_groups"][try(notification.value.lz_key, var.client_config.landingzone_key)] : value.id
if contains(notification.value.contact_groups_keys, key)
]), [])
)
contact_roles = try(notification.value.contact_roles, [])
enabled = try(notification.value.enabled, true)
}
}
}
- Extract the
monitor_action_groups
groups from thelocal_combined_resources
object by usingvar.local_combined_resources["monitor_action_groups"]
- Similar to the resource group pattern, the
for
iterates through themonitor_action_groups
and extracts the value that has a correspondingkey
that is part of the list of keys that was declared inconfiguration.tfvars
and is referenced asnotification.value.contact_groups_keys
in this file. Theid
can then be extracted from this value.
You can run this example directly using Terraform or via rover:
#Login to your Azure subscription
az login
#Run the example
cd /tf/caf/examples
terraform init
terraform [plan | apply | destroy] \
-var-file ./consumption_budget/102-consumption-budget-rg-alerts/configuration.tfvars
To test this deployment in the example landingzone, make sure the launchpad has been deployed first, then run the following command:
rover \
-lz /tf/caf/examples \
-var-folder /tf/caf/examples/consumption_budget/102-consumption-budget-rg-alerts/ \
-level level1 \
-a [plan | apply | destroy]