【问题标题】:azure with terraform multiple rules for security groupazure 与 terraform 安全组的多个规则
【发布时间】:2020-10-06 17:09:19
【问题描述】:

我需要为 Azure 资源设置多个安全规则。

在 AWS 上,我可以做多个入口:

  resource "aws_security_group" "mygroup" {
      name        = "mygroup"

      ingress {
        description = "allow all on ssh port"
        from_port   = var.ssh 
        to_port     = var.ssh 
        protocol    = "tcp"
        cidr_blocks = ["0.0.0.0/0"]
      }

      ingress {
        description = "public port"
        from_port   = var.public
        to_port     = var.public
        protocol    = "tcp"
        cidr_blocks =  ["0.0.0.0/0"]
      }

      ingress {
        description = "restricted"
        from_port   = var.restricted
        to_port     = var.restricted
        protocol    = "tcp"
        cidr_blocks =  ["<restricted-ip>/32"]
      }

但我不知道如何在 Azure 上执行此操作。

据我所知azurerm_network_security_group 只允许一个security_rule(这是正确的吗?)。

也许我可以为同一个network_interface_id 但不同的network_security_group_id 创建多个azurerm_network_interface_security_group_association

【问题讨论】:

    标签: azure terraform


    【解决方案1】:

    您为每个添加的规则使用 azurerm_network_security_rule 资源,如下所示:(引用自 azurerm_network_security_rule resource docs 的示例)

    resource "azurerm_network_security_group" "example" {
      name                = "acceptanceTestSecurityGroup1"
      location            = azurerm_resource_group.example.location
      resource_group_name = azurerm_resource_group.example.name
    }
    
    resource "azurerm_network_security_rule" "example" {
      name                        = "test123"
      priority                    = 100
      direction                   = "Outbound"
      access                      = "Allow"
      protocol                    = "Tcp"
      source_port_range           = "*"
      destination_port_range      = "*"
      source_address_prefix       = "*"
      destination_address_prefix  = "*"
      resource_group_name         = azurerm_resource_group.example.name
      network_security_group_name = azurerm_network_security_group.example.name
    }
    

    从技术上讲,您可以内联定义它们,但您不应该这样做,因为如果需要,其他模块无法添加安全组规则。根据我的经验,这在实践中经常发生,所以请不要使用内联规则,使用单独的资源。你的同事,包括你未来的自己,都会感谢你。

    【讨论】:

    • 哦,我明白了,所以我可以创建多个 azurerm_network_security_rule 实例,据我所知。看起来很棒!感谢您的提示!
    猜你喜欢
    • 2022-06-30
    • 2019-07-11
    • 1970-01-01
    • 2019-07-28
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    相关资源
    最近更新 更多