azure - 如何使用 Azure 中的 Terraform 创建具有多个协议(protocol)的安全规则?

标签 azure terraform

我的计划是允许协议(protocol) ICMP 和 TCP 使用相同的安全规则,但我遇到了与“属性值类型”相关的问题

我的 Terraform 代码:​​

resource "azurerm_network_security_group" "example" {
  name                = "01-tf-SG"
  location            = azurerm_resource_group.main.location
  resource_group_name = azurerm_resource_group.main.name

  security_rule {
    name                       = "test123"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = **["Icmp", "Tcp"]**  ---> iT FAILS!!! 
    source_port_range          = "*"
    destination_port_range     = "*"
    source_address_prefix      = "172.16.25.10/32"
    destination_address_prefix = "10.0.1.10/32"
  }

我在 terraform 存储库中没有找到任何示例:https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_security_rule

能够在同一安全规则协议(protocol)字段上使用多个协议(protocol)。

最佳答案

正如Mark B在他的回答中列出的那样,您无法提供协议(protocol)列表。但是您可以使用 dynamic block因此它会创建两个规则,而不必单独定义它们并复制代码

resource "azurerm_network_security_group" "example" {
  name                = "01-tf-SG"
  location            = azurerm_resource_group.main.location
  resource_group_name = azurerm_resource_group.main.name

  dynamic "security_rule" {
    for_each = toset(["Icmp", "Tcp"])
    content {
      name                       = "test123"
      priority                   = 100
      direction                  = "Inbound"
      access                     = "Allow"
      protocol                   = security_rule.value
      source_port_range          = "*"
      destination_port_range     = "*"
      source_address_prefix      = "172.16.25.10/32"
      destination_address_prefix = "10.0.1.10/32"
    }
  }
}

关于azure - 如何使用 Azure 中的 Terraform 创建具有多个协议(protocol)的安全规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74494380/

相关文章:

如果失败,Azure管道重新运行任务

node.js - 尝试使用azure在nodejs中获取访问 token 时出现错误,AADSTS50058 : A silent sign-in request was sent but no user is signed in

azure - 检索 Azure 中的 NIC 和关联的 VM

mysql - 如何使用 Terraform 管理数据库服务器

Terraform resource_aws_vpc_endpoint Dns 列表为空

azure - 如何在 Azure Monitor 工作簿中创建缺失值(或零)的连续时间图表?

c# - 使用终结器关闭EventHubClient

terraform - 解决 terraform 中 EntityAlreadyExists 错误的最佳方法是什么?

dictionary - 带有嵌套 map 的 terraform 嵌套动态 block

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