在 TF 中引用地图对象

Referencing map objects in TF

我需要反馈。我有以下部分 tf 代码。我收到“错误:属性值类型不正确...属性值不合适“security_rule”:需要一组对象。”。看起来对地图值的引用设置不正确,但似乎无法弄清楚。我错过了什么?谢谢

示例代码

input.tfvars
-----------------

test_nsg = {
  "testnsg_1" = {
    location = "West US"
    rules = {
      "AllOutbound" = {
        priority                   = 300
        direction                  = "Outbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "*"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
      },
      "AllowSSH" = {
        priority                   = 400
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "22"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
      },
    }
  },
  "testnsg_2" = {
    rules = {}
  },
  "testnsg_3" = {
    rules = {
      "AllOutbound" = {
        priority                   = 500
        direction                  = "Outbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "*"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
      },
    }
  },
}


nsg.tf
-------

resource "azurerm_network_security_group" "nsg" {
  for_each = var.test_nsg

  name                = each.key
  location            = var.location
  resource_group_name = var.rg_name
  tags                = var.tags
  security_rule       = each.value.rules
}

它应该是 List of objects,在你的情况下它是一个对象映射。我认为以下应该有效:

resource "azurerm_network_security_group" "nsg" {
  for_each = var.test_nsg

  name                = each.key
  location            = var.location
  resource_group_name = var.rg_name
  tags                = var.tags
  security_rule       = values(each.value.rules)
}