forked from aztfmod/terraform-azurerm-caf
-
Notifications
You must be signed in to change notification settings - Fork 0
Multiple Required Blocks
GlennChia edited this page Jun 29, 2021
·
1 revision
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.
In the configuration.tfvars file:
notifications = {
default = {
enabled = true
threshold = 90.0
operator = "EqualTo"
}
contact_email = {
enabled = true
threshold = 90.0
operator = "EqualTo"
contact_emails = [
"foo@example.com",
"bar@example.com",
]
}
contact_role = {
enabled = true
threshold = 90.0
operator = "EqualTo"
contact_roles = [
"Owner",
]
}
}- In this example, there are 3
notificationblocks within thenotificationsblock
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.