1
votes

I think I got pretty bad logic!

I'm trying to associate my Azure network groups to my subnet

Here is my security group configuration

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)}"
}

From this count.index I'm trying to import

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)}"
}

But I'm not able to access network_security_group_id with [count.index] or map.

 * 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:

It will be very easy for terraforming experts.

Please help here,

1

1 Answers

0
votes

Kiran, assuming your variable nsg_names is a list you should be able to use the following -

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)}"
}

You should use the same in the other lookups you perform in the other resources.

You can read more about this interpolation function here.