title: How can I associate NSG's and Subnets being created by loops in Terraform?
body: <p>Here is the code I am using to create subnets and nsgs I want to associate the NSG and subnet in the same script but I am unable to understand how can I get subnet IDs and NSG IDs which are being produced here and use them in the association resource. Thanks in advance for the help !</p>
  
  <p><strong>First part of code</strong> this is being used to create n no of Subnets and NSGs depends upon the parameter</p>
  
  <pre><code>provider "azurerm" {
  
    version = "2.0.0"
    features {}
  }
  
  resource "azurerm_resource_group" "new-rg" {
    name     = var.rg_name
    location = "West Europe"
  }
  
  resource "azurerm_virtual_network" "new-vnet" {
    name                = var.vnet_name
    address_space       = ["${var.vnet_address_space}"]
    location            = azurerm_resource_group.new-rg.location
    resource_group_name = azurerm_resource_group.new-rg.name
  
  
  }
  
  resource "azurerm_subnet" "test" {
    count                = "${length(var.subnet_prefix)}"
    name                 = "${element(var.subnet_subnetname, count.index)}"
    resource_group_name  = azurerm_resource_group.new-rg.name
    virtual_network_name = azurerm_virtual_network.new-vnet.name
    address_prefix       = "${element(var.subnet_prefix, count.index)}"
  
  }
  
  
  resource "azurerm_network_security_group" "new-nsg" {
  
      count        =  "${length(var.subnet_prefix)}"
    name                = "${element(var.subnet_subnetname, count.index)}-nsg"
    location            = azurerm_resource_group.new-rg.location
    resource_group_name = azurerm_resource_group.new-rg.name
  }
  </code></pre>
  
  <p>Below is the resource where i have to pass the parameters to create the association for the above subnets and nsgs being created.</p>
  
  <p><strong>Second Part of code</strong> Need to make the below code usable for above solution for n no of associations.</p>
  
  <pre><code>resource "azurerm_subnet_network_security_group_association" "example" {
  
    subnet_id                 = azurerm_subnet.example.id
    network_security_group_id = azurerm_network_security_group.example.id
  }
  </code></pre>
  
  <p>How can associate the n number of subnets and nsgs being created by using 2nd part of code, I cant find my way to that</p>