title: Associate the NSG with the subnet
body: <p>I am confused as how to associate the NSG with the subnet within my configuration.</p>
  <p>Created a data and azurerm_subnet_network_security_group_association block but I'm sure the config is not correct.</p>
  <p>Is someone able to review my code and give me some guidance.
  Thanks</p>
  <p><strong>app1-data.tf</strong></p>
  <pre><code>data &quot;azurerm_subnet&quot; &quot;subnet_data&quot; {
    name                 = var.subnet_name
    virtual_network_name = var.net_name
    resource_group_name = var.resource_group_name
  }
  </code></pre>
  <p><strong>app1-networking_nsgs.tf</strong></p>
  <pre><code>module &quot;nsg-app1&quot; {
    source = &quot;git@gitlab.com:*/*&quot;
    nsg_name            = var.nsg_name
    resource_group_name = var.resource_group_name
    location            = var.location
    nsg_security_rules = var.nsg_security_rules
  }
  
  
  # Associate the NSG with the subnet
  resource &quot;azurerm_subnet_network_security_group_association&quot; &quot;subnet_association&quot; {
  
    # subnet_id                 = var.subnet_id
      subnet_id                 = data.azurerm_subnet.subnet_data.subnet.id
      network_security_group_id = data.azurerm_network_security_group.nsg_data.id
    # network_security_group_id = data.azurerm_network_security_group.nsg_data[0].id
  }
  </code></pre>
  <p><strong>module-subnet-main.tf</strong></p>
  <pre><code># Create the Subnet
  resource &quot;azurerm_subnet&quot; &quot;subnet&quot; {
    name                 = var.subnet_names
    resource_group_name  = var.resource_group_name
    virtual_network_name = var.vnet_name
    address_prefixes     = var.subnet_cidr_list
  }
  </code></pre>
  <p><strong>module-subnet-outputs.tf</strong></p>
  <pre><code>output &quot;subnet_name&quot; {
    description = &quot;Name of the created subnet&quot;
    value       = azurerm_subnet.subnet.name
  }
  
  output &quot;subnet_id&quot; {
    value = azurerm_subnet.subnet.id
  }
  
  output &quot;subnet_cidr_list&quot; {
    value = azurerm_subnet.subnet.address_prefixes
  }
  </code></pre>
  <p><strong>module-subnet-variables.tf</strong></p>
  <pre><code>variable &quot;subnet_names&quot; {
    type = string
  }
  
  variable &quot;resource_group_name&quot; {
    type        = string
    description = &quot;name of resource group&quot;
  }
  
  variable &quot;subnet_cidr_list&quot; {
    type        = list(any)
    description = &quot;Address prefixes of Subnet&quot;
  }
  
  variable &quot;vnet_name&quot; {
    type        = string
    description = &quot;Name of Virtual Network&quot;
  }
  </code></pre>
  <p><strong>module-nsg-main.tf</strong></p>
  <pre><code>resource &quot;azurerm_network_security_group&quot; &quot;nsg&quot; {
    name                = var.nsg_name
    resource_group_name = var.resource_group_name
    location            = var.location
    # tags                = var.tags
  
    dynamic &quot;security_rule&quot; {
      for_each = var.nsg_security_rules
      content {
        name                                       = lookup(security_rule.value, &quot;name&quot;, null)
        priority                                   = lookup(security_rule.value, &quot;priority&quot;, null)
        direction                                  = lookup(security_rule.value, &quot;direction&quot;, null)
        access                                     = lookup(security_rule.value, &quot;access&quot;, null)
        protocol                                   = lookup(security_rule.value, &quot;protocol&quot;, null)
        source_port_range                          = lookup(security_rule.value, &quot;source_port_range&quot;, null)
        source_port_ranges                         = lookup(security_rule.value, &quot;source_port_ranges&quot;, null)
        destination_port_range                     = lookup(security_rule.value, &quot;destination_port_range&quot;, null)
        destination_port_ranges                    = lookup(security_rule.value, &quot;destination_port_ranges&quot;, null)
        source_address_prefix                      = lookup(security_rule.value, &quot;source_address_prefix&quot;, null)
        source_address_prefixes                    = lookup(security_rule.value, &quot;source_address_prefixes&quot;, null)
        destination_address_prefix                 = lookup(security_rule.value, &quot;destination_address_prefix&quot;, null)
        destination_address_prefixes               = lookup(security_rule.value, &quot;destination_address_prefixes&quot;, null)
        source_application_security_group_ids      = lookup(security_rule.value, &quot;source_application_security_group_ids &quot;, null)
        destination_application_security_group_ids = lookup(security_rule.value, &quot;destination_application_security_group_ids &quot;, null)
      }
    }
  }
  </code></pre>
  <p><strong>module-nsg-outputs.tf</strong></p>
  <pre><code>output &quot;nsg_id&quot; {
    description = &quot;The ID of the newly created Network Security Group&quot;
    value       = azurerm_network_security_group.nsg.id
  }
  
  output &quot;nsg_name&quot; {
    description = &quot;The name of the new NSG&quot;
    value       = azurerm_network_security_group.nsg.name
  }
  </code></pre>
  <p><strong>module-nsg-variables.tf</strong></p>
  <pre><code>variable &quot;resource_group_name&quot; {
    description = &quot;description&quot;
    type        = string
  }
  
  variable &quot;location&quot; {
    description = &quot;description&quot;
    type        = string
    # default     = &quot;West Europe&quot;
  }
  
  variable &quot;nsg_name&quot; {
    description = &quot;description&quot;
    type        = string
  }
  
  variable &quot;nsg_security_rules&quot; {
    description = &quot;A list of security rules to add to the security group. Each rule should be a map of values to add. See the Readme.md file for further details.&quot;
  
    type = list(object({
      name                       = string
      priority                   = number
      direction                  = string
      access                     = string
      protocol                   = string
      source_port_range          = string
      destination_port_range     = string
      source_address_prefix      = string
      destination_address_prefix = string
    }))
  }
  </code></pre>
  <p>❯ terraform plan -var-file=dev.tfvars</p>
  <pre><code>Error: Unsupported attribute
  │ 
  │   on networking_nsgs.tf line 19, in resource &quot;azurerm_subnet_network_security_group_association&quot; &quot;subnet_association&quot;:
  │   19:     subnet_id                 = data.azurerm_subnet.subnet_data.subnet.id
  │ 
  │ This object has no argument, nested block, or exported attribute named &quot;subnet&quot;.
  ╵
  ╷
  │ Error: Reference to undeclared resource
  │ 
  │   on networking_nsgs.tf line 20, in resource &quot;azurerm_subnet_network_security_group_association&quot; &quot;subnet_association&quot;:
  │   20:     network_security_group_id = azurerm_network_security_group.nsg.id
  │ 
  │ A managed resource &quot;azurerm_network_security_group&quot; &quot;nsg&quot; has not been declared in the root module.
  </code></pre>