terraform - 此处不能使用each.value

标签 terraform terraform-provider-azure

我收到以下错误:

│ A reference to "each.value" has been used in a context in which it
│ unavailable, such as when the configuration no longer contains the value in
│ its "for_each" expression. Remove this reference to each.value in your
│ configuration to work around this error.

我想使用 for_each 循环和 every.value 来缩短我的代码。我已将其添加到变量中:

variable "log_categories" {
    type = list(string)
    default = ["AZFWApplicationRule", "AZFWApplicationRuleAggregation", "AZFWDnsQuery", "AZFWFqdnResolveFailure", "AZFWIdpsSignature", "AZFWNatRule", "AZFWNatRuleAggregation", "AZFWNetworkRule", "AZFWNetworkRuleAggregation", "AZFWThreatIntel", "AzureFirewallApplicationRule", "AzureFirewallDnsProxy", "AzureFirewallNetworkRule", "AllMetrics"]
}

这就是我使用 for.each 循环的地方:

  log {
      for_each = toset(var.log_categories)
      category = each.value
      retention_policy {
          days    = 30
          enabled = false
      }
  }
}

不知道哪里出了问题。我已经尝试使用each.key而不是each.value,但也不起作用

最佳答案

我必须创建一个动态 block 才能使其工作:

  dynamic "log" {
      for_each = toset(var.log_categories)
      content {
        category = log.value
        retention_policy {
          days    = 30
          enabled = false
      }
    }
  }
}

关于terraform - 此处不能使用each.value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73154414/

相关文章:

terraform - 如何从 aws secret 管理器中检索 terraform 中的 secret

amazon-web-services - 带有 Terraform 的 AWS - 安全组规则中的安全组参数

terraform - aws_lb_target_group_attachment 期间 for_each 参数无效

azure - 使用 Terraform 的同一 VNet 中的多个子网

azure - 从 Terraform 获取数据库 URL `azurerm_postgresql_flexible_server`

Terraform - 在 Azure 上配置静态 IP 地址

Azure : GroupsClient. BaseClient.Get():意外状态 403,带有 OData 错误:Authorization_RequestDenied:权限不足

azure - 使用 Terraform 在 Azure 中部署和配置 Postgres 服务器

json - DevOps Pipeline 在将变量值作为 JSON 正文发送以使用 Terraform 部署 Azure 资源时更改时间格式

azure - 将 Terraform Locals 用于列表对象 {type = list(object({}))}