Terraform:在嵌套动态块中嵌套 for_each

问题描述 投票:0回答:1

我有以下变量定义:

variable "rule-configuration" {
  description = "Configuration of the Firewall rules. The key is the name of the rule"
  type = map(object({
    priority           = number
    hub_prefix = string
    spoke_prefixes = list(object({
      name = string
      cidr = string
    }))
  }))
}

我有以下资源定义:

resource "azurerm_firewall_policy_rule_collection_group" "this" {
  name               = "xxx"
  firewall_policy_id = "xxx"
  priority           = 500

  dynamic "network_rule_collection" {
    for_each = var.firewall_configuration

    content {
      name     = each.key
      priority = each.value.priority
      action   = "Allow"

      dynamic "rule" {
        for_each = each.value.spoke_prefixes

        content {
          name                  = ??.value.name
          source_addresses      = [each.value.hub_prefix]
          destination_addresses = [??.value.cidr]
          protocols             = ["Any"]
          destination_ports     = ["*"]
        }
      }
    }
  }
}

如何使嵌套动态块中的嵌套 for_each 工作?

terraform terraform-provider-azure
1个回答
0
投票

Terraform 允许指定迭代器,允许将其用于嵌套的 for_each:

resource "azurerm_firewall_policy_rule_collection_group" "this" {
  name               = "xxx"
  firewall_policy_id = "xxx"
  priority           = 500

  dynamic "network_rule_collection" {
    for_each = var.firewall_configuration
    iterator = firewall_configuration

    content {
      name     = firewall_configuration.key
      priority = firewall_configuration.value.priority
      action   = "Allow"

      dynamic "rule" {
        for_each = each.value.spoke_prefixes
        iterator = rule

        content {
          name                  = rule.value.name
          source_addresses      = [each.value.hub_prefix]
          destination_addresses = [rule.value.cidr]
          protocols             = ["Any"]
          destination_ports     = ["*"]
        }
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.