TERRAFORM如何在子网中设置count.index以检索network_security_group_id

时间:2018-10-15 02:34:53

标签: azure terraform terraform-provider-azure

我认为我的逻辑很糟糕!

我正在尝试将Azure网络组关联到我的子网

这是我的安全组配置

resource "azurerm_network_security_group" "azure_nsg" {
  depends_on = ["azurerm_virtual_network.vnet"]
  location = "${var.region}"
  name = "${var.nsg_names[count.index]}" // I think my problem is here
  resource_group_name = "${azurerm_resource_group.vnet.name}"
  count = "${length(var.nsg_names)}"
}

我正在尝试从此count.index导入

resource "azurerm_subnet" "subnet" {
  depends_on = ["azurerm_network_security_group.private"]
  name                      = "${var.subnet_names[count.index]}"
  virtual_network_name      = "${azurerm_virtual_network.vnet.name}"
  resource_group_name       = "${azurerm_resource_group.vnet.name}"
  address_prefix            = "${var.subnet_prefixes[count.index]}"
  network_security_group_id = "${lookup(var.nsg_ids,var.subnet_names[count.index],"")}"
  count                     = "${length(var.subnet_names)}"
}

但是我无法通过[count.index]或地图访问network_security_group_id

 * azurerm_subnet.subnet.1: [ERROR] Unable to Parse Network Security Group ID 'databasensg': Cannot parse Azure ID: parse databasensg: invalid URI for request
    * module.azure_vnet.azurerm_subnet.subnet[2]: 1 error(s) occurred:

    * azurerm_subnet.subnet.2: [ERROR] Unable to Parse Network Security Group ID 'jumpboxnsg': Cannot parse Azure ID: parse jumpboxnsg: invalid URI for request
    * module.azure_vnet.azurerm_network_security_group.azure_nsg[1]: 1 error(s) occurred:

对于地形专家来说,这将非常容易。

请在这里帮助

1 个答案:

答案 0 :(得分:0)

基兰(Kiran),假设您的变量nsg_names是列表,您应该可以使用以下代码-

resource "azurerm_network_security_group" "azure_nsg" {
  depends_on = ["azurerm_virtual_network.vnet"]
  location = "${var.region}"
  name = "${element(var.nsg_names, count.index)}"
  resource_group_name = "${azurerm_resource_group.vnet.name}"
  count = "${length(var.nsg_names)}"
}

您应该在其他资源中执行的其他查找中使用相同的内容。

您可以了解有关此插值函数here的更多信息。