-
Notifications
You must be signed in to change notification settings - Fork 705
Multiple Required Blocks
GlennChia edited this page Aug 8, 2021
·
2 revisions
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 = {
contact_email = {
enabled = true
threshold = 90.0
operator = "EqualTo"
contact_emails = [
"foo@example.com",
"bar@example.com",
]
}
contact_role = {
enabled = true
threshold = 85.0
operator = "EqualTo"
contact_roles = [
"Owner",
]
}
}
- In this example, there are 2
notification
blocks within thenotifications
block - Note: Specifically for this example, each notification block should have a unique
threshold
andoperator
combination. 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_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.