title: terraform multiple NSG using csv
body: <p>I am trying to follow the solution given here <a href="https://stackoverflow.com/questions/60406160/how-i-can-avoid-multiple-loops-with-terraform-for-each-and-dynamic-for-resource#comment107496004_60539818">How I can avoid multiple loops with Terraform for_each and dynamic for resource azurerm_network_security_group?</a></p>
  <p>Here is my module: Module-Azure-Nsg-V3/main.tf</p>
  <pre><code>locals {
    # read csv file
    list_of_csv_lines = csvdecode(file(var.csv_file_name))
    # collect all nsg names , (Unique)
    all_nsg_names = distinct([for item in local.list_of_csv_lines : item.nsg_name])
    # collect all nsg names other syntax , (Unique)
    all_nsg_names_again = distinct(local.list_of_csv_lines[*].nsg_name)
  
  
    # Loop over all unique names ---&gt; for item in local.all_nsg_names : 
    # create a key of nsg name item ----&gt; &quot;${item}&quot; =&gt;
    # And to now fill value in that dictionary item key, 
    # Loop over list of csv lines  
    # pick the line 
    # if nsg_name matches item
    combine = {
        for item in local.all_nsg_names : 
          &quot;${item}&quot; =&gt; [
            for line in local.list_of_csv_lines : line 
            if item == line.nsg_name
          ]
    }
    
  
  
  }
  
  resource &quot;azurerm_network_security_group&quot; &quot;this&quot; {
    for_each = local.combine
  
    name                = each.key
    location            = var.location
    resource_group_name = var.resource_group_name
    dynamic &quot;security_rule&quot; {
       for_each = each.value
      
          content {
                  name                        = security_rule.value[&quot;nsg_name&quot;]
                  priority                    = security_rule.value[&quot;priority&quot;]
                  direction                   = security_rule.value[&quot;direction&quot;]
                  access                      = security_rule.value[&quot;access&quot;]
                  protocol                    = security_rule.value[&quot;protocol&quot;]
                  source_port_range           = security_rule.value[&quot;source_port_range&quot;]
                  destination_port_range      = security_rule.value[&quot;destination_port_range&quot;]
                  source_address_prefix       = security_rule.value[&quot;source_address_prefix&quot;]
                  destination_address_prefix  = security_rule.value[&quot;destination_address_prefix&quot;]
            }
    }
  
  }
  
  
  
  output &quot;all-nsg-names-in-output&quot; {
   value = local.all_nsg_names
  }
  output &quot;all-nsg-names-in-output-again&quot; {
   value = local.all_nsg_names_again
  }
  output &quot;combine-output&quot;{
    value = local.combine
  }
  </code></pre>
  <p>Here is my module: Module-Azure-Nsg-V3/variables.tf</p>
  <pre><code>variable &quot;csv_file_name&quot; {
    type = string
    default = null
  }
  
  variable &quot;resource_group_name&quot; {
    description = &quot;Name of the resource Group&quot;
    type = string
    default = null
  }
  
  variable &quot;location&quot; {
     description = &quot;enviroment in which you are working&quot;
     type = string
     default = null
  }
  </code></pre>
  <p>Now I am using above mention module to create NSG.</p>
  <p>Here is my nsg_rules.csv</p>
  <pre><code>nsg_name,rulename,priority,direction,access,protocol,source_port_range,destination_port_range,source_address_prefix,destination_address_prefix 
  nsg01,Rule01,100,Inbound,Allow,Tcp,80,*,*,*
  nsg01,Rule02,110,Inbound,Allow,Tcp,443,*,*,*
  nsg02,Rule01,100,Inbound,Allow,Tcp,80,*,*,*
  nsg02,Rule02,110,Inbound,Allow,Tcp,443,*,*,*
  nsg03,Rule01,100,Inbound,Allow,Tcp,80,*,*,*
  </code></pre>
  <p>This is variables.tf file</p>
  <pre><code>variable &quot;csv_file_name&quot; {
    type = string
    default = &quot;nsg_rules.csv&quot;
  }
  
  variable &quot;location&quot; {
     description = &quot;enviroment in which you are working&quot;
     type = string
     default = &quot;eastus&quot;
  }
  
  
  variable &quot;resource_group_name&quot; {
    description = &quot;Name of the resource Group&quot;
    type = string
    default = &quot;terraform_rg&quot;
  }
  </code></pre>
  <p>And main.tf</p>
  <pre><code>module &quot;Module-Azure-Nsg-V3&quot; {
    source  = &quot;./Module-Azure-Nsg-V3&quot;
    csv_file_name = var.csv_file_name
    location = var.location
    resource_group_name = var.resource_group_name
   }
  
  output &quot;display_this&quot; {
     value = module.Module-Azure-Nsg-V3.all-nsg-names-in-output
  }
  
  output &quot;display_this_again&quot; {
     value = module.Module-Azure-Nsg-V3.all-nsg-names-in-output-again
  }
  output &quot;all-nsg-rules&quot; {
     value = module.Module-Azure-Nsg-V3.combine-output
  }
  </code></pre>
  <p>But I get errors.</p>
  <pre><code>╷
  │ Error: Invalid index
  │ 
  │   on Module-Azure-Nsg-V3/main.tf line 46, in resource &quot;azurerm_network_security_group&quot; &quot;this&quot;:
  │   46:                 destination_address_prefix  = security_rule.value[&quot;destination_address_prefix&quot;]
  │     ├────────────────
  │     │ security_rule.value is object with 10 attributes
  │ 
  │ The given key does not identify an element in this collection value.
  </code></pre>