Referencing output of one module in another module when module is using for_each

I have created a module network resources . Below is the code used:

locals.tf

locals {

subnets_flatlist = flatten([for key, val in var.vnets : [

for subnet in val.subnets : {

  vnet_name      = key

  subnet_name    = subnet.subnet_name

  subnet_address = subnet.subnet_address

}

]

])

subnets = { for subnet in local.subnets_flatlist : subnet.subnet_name => subnet }

}

main.tf

data “azurerm_resource_group” “network” {

name = var.resource_group_name

}

resource “azurerm_virtual_network” “vnets” {

for_each = var.vnets

name = each.key

resource_group_name = data.azurerm_resource_group.network.name

location = data.azurerm_resource_group.network.location

address_space = [each.value.address_space]

}

resource “azurerm_subnet” “subnets” {

for_each = local.subnets

name = each.value.subnet_name

resource_group_name = data.azurerm_resource_group.network.name

virtual_network_name = azurerm_virtual_network.vnets[each.value.vnet_name].name

address_prefixes = [each.value.subnet_address]

}

output.tf

output “vnet_names” {

description = “The name of the virtual networks”

value = {for k, v in azurerm_virtual_network.vnets: k => v.name}

}

output “vnet_addresses” {

description = “The name of the virtual networks”

value = {for k, v in azurerm_virtual_network.vnets: k => v.address_space}

}

output “subnet_names” {

description = “The name of the subnets”

value = {for k, v in azurerm_subnet.subnets: k => v.name}

}

output “subnet_addresses” {

description = “The name of the subnet addresses”

value = {for k, v in azurerm_subnet.subnets: k => v.address_prefixes}

}

output “subnet_ids” {

description = “The name of the subnet ids”

value = {for k, v in azurerm_subnet.subnets: k => v.id}

}

variables.tf

variable “resource_group_name” {

description = “Name of the resource group to be imported.”

type = string

}

variable “location” {

description = “The location of the vnet to create. Defaults to the location of the resource group.”

type = string

default = null

}

variable “vnets” {

type = map(object({

address_space = string

subnets = list(object({

  subnet_name    = string

  subnet_address = string

}))

}))

default = {

"bupavnet1" = {

  address_space = "192.168.0.0/16",

  subnets       = []

},

"bupavnet2" = {

  address_space = "10.0.0.0/16",

  subnets = [

    {

      subnet_name    = "subnet1_bupavnet1"

      subnet_address = "10.0.2.0/24"

    },

    {

      subnet_name    = "subnet2_bupavnet1"

      subnet_address = "10.0.0.0/24"

    }

  ]

},

"bupavnet3" = {

  address_space = "10.80.0.0/16"

  subnets = [

    {

      subnet_name    = "subnet1_bupavnet3"

      subnet_address = "10.80.2.0/24"

    },

    {

      subnet_name    = "subnet2_bupavnet3"

      subnet_address = "10.80.1.0/24"

    },

    {

      subnet_name    = "subnet3_bupavnet3"

      subnet_address = "10.80.0.0/24"

    },

  ]

}

}

}

I have a nsg module where i need to pass the subnet id which will be created from the above module. The output defined here is creating a group of subnet ids. Kindly help me in using one of those subnet ids in my nsg module.