Multiple Required Blocks - ivaramme/terraform-azurerm-caf GitHub Wiki
This patten covers the following phrase in the Terraform Registry:
(Required) One or more <name of block> blocks as defined below.
Example:
(Required) One or more notification blocks as defined below.
Simple use case
In the configuration.tfvars file:
notifications = {
contact_email = {
enabled = true
threshold = 90.0
operator = "EqualTo"
contact_emails = [
"[email protected]",
"[email protected]",
]
}
contact_role = {
enabled = true
threshold = 85.0
operator = "EqualTo"
contact_roles = [
"Owner",
]
}
}
- In this example, there are 2
notificationblocks within thenotificationsblock - Note: Specifically for this example, each notification block should have a unique
thresholdandoperatorcombination. If it's intended for them to be the same then one notification block can support a combination ofcontact_emails,contact_roles, andcontact_groups.
In the resource file:
dynamic "notification" {
for_each = var.settings.notifications
content {
operator = notification.value.operator
threshold = notification.value.threshold
contact_emails = try(notification.value.contact_emails, [])
contact_roles = try(notification.value.contact_roles, [])
enabled = try(notification.value.enabled, true)
}
}
- The
for_eachiterates through eachnotificationblock within thenotificationsobject - In this
for_each, thenotificationsis not destructured into itskeyandvaluebecause this is a required block and we want it to produce an error if anotificationsvariable is not injected into the resource.