【问题标题】:How do I create multiple Security rules using Terraform in Azure?如何在 Azure 中使用 Terraform 创建多个安全规则?
【发布时间】:2019-07-11 15:53:14
【问题描述】:

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

"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"
    }
}

【问题讨论】:

    标签: azure terraform terraform-provider-azure


    【解决方案1】:

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

    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

    【讨论】:

    • 谢谢。我刚刚意识到这一点并使用了资源“azurerm_network_security_rule”,我现在很好。
    • 一个小修正。优先级应该是以下优先级=“${(100 * (count.index + 1))}”下面抛出错误:错误:azurerm_network_security_rule.k8hway[0]:优先级:无法将''解析为int:strconv。 ParseInt: 解析 "(100 * (0 + 1))": 无效语法优先级 = "(100 * (${count.index} + 1))"
    • 我没有看你的代码,只是告诉你如何创建一个循环,代码是你问题的复制粘贴
    • 是的,更正是针对任何想使用此解决方案的人。我并不是要指出您的解决方案。再次感谢
    猜你喜欢
    • 1970-01-01
    • 2020-10-06
    • 1970-01-01
    • 2020-10-15
    • 2021-06-16
    • 2020-08-20
    • 1970-01-01
    • 2022-09-23
    • 2020-03-27
    相关资源
    最近更新 更多