如何在 Azure 中使用 Terraform 创建多个安全规则?

How do I create multiple Security rules using Terraform in Azure?

我正在尝试创建一个包含多个安全规则的网络安全组。这个想法是创建一个列表变量(端口范围)并在 .tf 文件中插入列表项。下面的脚本抛出一个错误“priority.

"Error: azurerm_network_security_group.k8hway: security_rule.0: invalid or unknown key: count"

下面是 Terraform 代码:​​

resource "azurerm_network_security_group" "NSG" {
  name     = "NSG-Demo"
  location = "${azurerm_resource_group.main.location}"
  resource_group_name  = "${azurerm_resource_group.main.name}"

  security_rule  {
      count = "${length(var.inbound_port_ranges)}"
      name                       = "sg-rule-${count.index}"
      direction                  = "Inbound"
      access                     = "Allow"
      priority                   = "(100 * (${count.index} + 1))"
      source_address_prefix      = "*"
      source_port_range          = "*"
      destination_address_prefix = "*"
      destination_port_range     = "${element(var.inbound_port_ranges, count.index)}"
      protocol                   = "TCP"
    }
}

我认为属性不支持计数,但资源支持。使用网络安全组规则:

resource "azurerm_network_security_rule" "test" {
  count = "${length(var.inbound_port_ranges)}"
  name                       = "sg-rule-${count.index}"
  direction                  = "Inbound"
  access                     = "Allow"
  priority                   = "(100 * (${count.index} + 1))"
  source_address_prefix      = "*"
  source_port_range          = "*"
  destination_address_prefix = "*"
  destination_port_range     = "${element(var.inbound_port_ranges, count.index)}"
  protocol                   = "TCP"
}

阅读中:

https://www.terraform.io/docs/providers/azurerm/r/network_security_rule