Terraform中如何将sensitive_labels设为可选dynamic block?
问题:如何优雅实现Terraform中sensitive_labels动态块的可选属性?
我尝试将sensitive_labels动态块设置为可选属性但未成功,现有代码如下:
现有代码
variables.tf
variable "notification_channels" { type = any } variable "project_id" { type = string }
main.tf(初始报错版本)
resource "google_monitoring_notification_channel" "channels" { project = var.project_id for_each = { for k, v in var.notification_channels : k => v } type = each.value.type display_name = each.value.display_name description = each.value.description labels = each.value.labels enabled = each.value.enabled dynamic "sensitive_labels" { for_each = each.value.sensitive_labels != {} ? [each.value.sensitive_labels] : [] content { auth_token = lookup(sensitive_labels.value, "auth_token", null) } } }
dev.tfvars
notification_channels = [ { type = "email" display_name = "a channel to send emails" description = "a nice channel" labels = { email_address = "HeyThere@something.com" } enabled = true sensitive_labels = {} // 此条目无报错 }, { type = "email" display_name = "HeyThere Email" description = "a channel to send emails" labels = { email_address = "HeyThere2@something.com" } enabled = true } ]
报错信息
Error: Unsupported attribute
on notification_channels.tf line 11, in resource "google_monitoring_notification_channel" "channels":
11: for_each = each.value.sensitive_labels != {} ? [each.value.sensitive_labels] : []
│ ├────────────────
each.value is object with 5 attributesThis object does not have an attribute named "sensitive_labels".
临时解决方案(lookup实现)
我用lookup方法实现了功能,但感觉不够优雅:
resource "google_monitoring_notification_channel" "channels" { project = var.project_id for_each = { for k, v in var.notification_channels : k => v } type = each.value.type display_name = each.value.display_name description = each.value.description labels = each.value.labels enabled = each.value.enabled dynamic "sensitive_labels" { for_each = lookup(each.value, "sensitive_labels", {}) content { auth_token = lookup(sensitive_labels.value, "auth_token", null) } } }
请问有没有更优的实现方式?
内容的提问来源于stack exchange,提问作者MassHysteria
相关产品推荐
相关产品推荐

