'Using multiple module outputs to a new module in Terraform

I have seen some codes with same intention but somehow I couldnt make it work. I have two different modules,

  1. subnet - Where I'm creating two subnets where subnet name is provided in tfvars
  2. nsg - Where I'm creating two nsg where nsg name is provided in tfvars

And I output the created subnet id's and nsg_ids to my main.tf from both module

What I'm trying to do is to associate each subnets to each nsg's like

  • subnet1 to nsg1
  • subnet2 to nsg2

Main.tf

module "nsg" {
  source              = "./Modules/NSGConfig"
  nsglist             = var.nsglist
  resource_group_name = azurerm_resource_group.resource_group.name
  location            = azurerm_resource_group.resource_group.location
  nsg = tomap(
    {
      for k, subnet_id in module.SUBNETS.subnet_ids : k =>
      {
        subnet_id = subnet_id 
      }
    }
  )
}

NSG.tf (only including association part)

resource "azurerm_subnet_network_security_group_association" "nsg_association" {
  for_each=var.nsg
  subnet_id                 = each.value.subnet_id
  network_security_group_id = azurerm_network_security_group.nsg[*].nsg_id  #wont work
}

variable.tf (NSG module)

variable "nsg" {
  type = map(object({
    subnet_id = string
    }))
}

I tried to nest the for (in main.tf) to include the output from nsgid but failed.

Ps. I'm really new to terraform

Main.tfvars

RGlocation = "westus"
RGname     = "TEST-RG1-TERRAFORM"

VNETname      = "TEST-VNET-TERRAFORM"
address_space = "10.0.0.0/16"

Subnetlist = {
  "s1" = { name = "TESTSUBNET1-TERRAFORM", address = "10.0.1.0/24" },
  "s2" = { name = "TESTSUBNET2-TERRAFORM", address = "10.0.2.0/24" },
  "s3" = { name = "TESTSUBNET3-TERRAFORM", address = "10.0.3.0/24" }
}

niclist = {
  "s1" = { name = "TESTNIC1-TERRAFORM" },
  "s2" = { name = "TESTNIC2-TERRAFORM" },
  "s3" = { name = "TESTNIC3-TERRAFORM" }
}

nsglist = {
  "s1" = { name = "TESTNSG1-TERRAFORM" },
  "s2" = { name = "TESTNSG1-TERRAFORM" },
  "s3" = { name = "TESTNSG1-TERRAFORM" }
}

--- Update 2

Module output from the subnet module and NSG module is as below

Outputs:

nsg_id = tomap({
  "s1" = "./resourceGroups/TEST-RG1-TERRAFORM/providers/Microsoft.Network/networkSecurityGroups/TESTNSG1-TERRAFORM"
  "s2" = "./resourceGroups/TEST-RG1-TERRAFORM/providers/Microsoft.Network/networkSecurityGroups/TESTNSG1-TERRAFORM"
  "s3" = "./resourceGroups/TEST-RG1-TERRAFORM/providers/Microsoft.Network/networkSecurityGroups/TESTNSG1-TERRAFORM"
})
sub_id = tomap({
  "s1" = "./resourceGroups/TEST-RG1-TERRAFORM/providers/Microsoft.Network/virtualNetworks/SACHIN-TEST-VNET-TERRAFORM/subnets/TESTSUBNET1-TERRAFORM"
  "s2" = "./resourceGroups/TEST-RG1-TERRAFORM/providers/Microsoft.Network/virtualNetworks/SACHIN-TEST-VNET-TERRAFORM/subnets/TESTSUBNET2-TERRAFORM"
  "s3" = "./resourceGroups/TEST-RG1-TERRAFORM/providers/Microsoft.Network/virtualNetworks/SACHIN-TEST-VNET-TERRAFORM/subnets/TESTSUBNET3-TERRAFORM"
})


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source