Multiple Required Blocks - GlennChia/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 = {
default = {
enabled = true
threshold = 90.0
operator = "EqualTo"
}
contact_email = {
enabled = true
threshold = 90.0
operator = "EqualTo"
contact_emails = [
"[email protected]",
"[email protected]",
]
}
contact_role = {
enabled = true
threshold = 90.0
operator = "EqualTo"
contact_roles = [
"Owner",
]
}
}
- In this example, there are 3
notification
blocks within thenotifications
block
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_each
iterates through eachnotification
block within thenotifications
object - In this
for_each
, thenotifications
is not destructured into itskey
andvalue
because this is a required block and we want it to produce an error if anotifications
variable is not injected into the resource.