【问题标题】:Adding conditional booleans to Terraform script向 Terraform 脚本添加条件布尔值
【发布时间】:2019-09-20 01:44:32
【问题描述】:

我正在尝试添加一个名为“DeployNSG”的变量作为真/假布尔值。当我使用“计数”在 NSG 的资源创建中引用变量时,我试图将 NSG 与 Azurerm_Network_security_group_association 与子网相关联,这表示我需要在关联中使用计数索引。但是如果我再尝试并使用 element 来引用一个项目,它表示如果在子网关联中未使用 count ,则不能使用 element。


resource "azurerm_network_security_group" "ProdNSG" {
  count = "${var.DeployNSG ? 1 : 0}"
  name                = "${var.ProdNSG}"
  location            = "${var.location}"
  resource_group_name = "${azurerm_resource_group.ProdNetworkRG.name}"

  security_rule {
    name                       = "AllowRDP"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "3389"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }
}

resource "azurerm_virtual_network" "ProdVNet" {
  name          = "${var.ProdVNet}"
  resource_group_name = "${azurerm_resource_group.ProdNetworkRG.name}"
  address_space = "${var.ProdVNetAddressSpace}"
  location      = "${var.location}"


}

resource "azurerm_subnet" "ServersSubnet" {
  resource_group_name = "${azurerm_resource_group.ProdNetworkRG.name}"
  name = "${var.ServersSubnet}"
  address_prefix = "${var.ServersSubnetAddressPrefix}"
  virtual_network_name = "${azurerm_virtual_network.ProdVNet.name}"

}

resource "azurerm_subnet_network_security_group_association" "ServersNSGAssociation" {
  subnet_id                 = "${azurerm_subnet.ServersSubnet.id}"
  network_security_group_id = "${azurerm_network_security_group.ProdNSG.id}"
}

如果我注释掉关联,则真/假条件有效,因此我相信这是卡住的地方。

【问题讨论】:

    标签: terraform terraform-provider-azure


    【解决方案1】:

    如果在某些情况下,一个资源的 count 可能为零,那么在您引用该资源的任何其他位置,您必须告诉 Terraform 如何处理另一个对象不存在的情况。

    在这种情况下,如果网络安全组不存在,您似乎根本不需要 azurerm_subnet_network_security_group_association 资源,因此最简单的答案是将相同的 count 应用于其他资源:

    resource "azurerm_network_security_group" "ProdNSG" {
      count = var.DeployNSG ? 1 : 0
    
      # ...other arguments as you already have set...
    }
    
    resource "azurerm_subnet_network_security_group_association" "ServersNSGAssociation" {
      # Create one of this resource only if there is one of the other resource.
      count = length(azurerm_network_security_group.ProdNSG)
    
      subnet_id                 = azurerm_subnet.ServersSubnet.id
      network_security_group_id = azurerm_network_security_group.ProdNSG[count.index].id
    }
    

    请注意,我们现在可以在引用azurerm_network_security_group.ProdNSG 时使用count.index,因为azurerm_subnet_network_security_group_association.ServersNSGAssociation 具有与count 相同的countazurerm_network_security_group.ProdNSG。当一个 NSG 存在时,count.index 将为 0,因此它将选择第一个(也是唯一一个)NSG 实例。当不存在 NSG 时,也不会存在 NSG 附件,因此永远不会评估 count.index

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 2020-12-15
      相关资源
      最近更新 更多