从具有多个子网的虚拟网络中选择特定的子网ID

时间:2019-07-19 18:51:37

标签: terraform terraform-provider-azure

我想在另一个资源块中引用subnet 2的ID,但是子网没有索引值。即“ $ {azurerm_virtual_network.test.subnet.2.id}”不起作用。

resource "azurerm_virtual_network" "test" {
  name                = "virtualNetwork1"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"
  address_space       = ["10.0.0.0/16"]
  dns_servers         = ["10.0.0.4", "10.0.0.5"]

  subnet {
    name           = "subnet1"
    address_prefix = "10.0.1.0/24"
  }

  subnet {
    name           = "subnet2"
    address_prefix = "10.0.2.0/24"
  }

  subnet {
    name           = "subnet3"
    address_prefix = "10.0.3.0/24"
    security_group = "${azurerm_network_security_group.test.id}"
  }
}

1 个答案:

答案 0 :(得分:0)

Expose ID of an embedded subnet defined in azurerm_virtual_network #1913的帮助下解决了

...
subnet_id = "${azurerm_virtual_network.test.subnet.*.id[1]}"
...