How to use outputs for for_each module

Hi Team,

So i am following this github repo(“learn-terraform-count-foreach/outputs.tf at master · hashicorp/learn-terraform-count-foreach · GitHub”) and i have some trouble using some outputs of for_each module.

There are 2 scenarios where i am unclear how to use

  1. Outputs to be mapped to a variable in next module which is declared as string
    So cd is a module which is run in for_each loop and it has output name pd. bd is a variable for a different module and it is declared as string and it can’t be changed to list.
    I tried
    bd = module.cd.pd[0]
    also
    bd = module.cd[0].pd
    get this error “module.cd is object with n attributes”
    bd = flatten ([{ for p in sort(keys(var.pm)) :
    p => module.cd[p].pd
    }])
    or
    bd = concat( [{ for p in sort(keys(var.pm)) :
    p => module.cd[p].pd
    }])

get this error “The given value is not suitable for child module variable
“bd” defined at
.: string required.”

Can anyone help ?

  1. We have a field where we need to combine output for 2 for_each module or 1 for_each module

Error: Invalid value for module argument

  PS = concat(
    module.fs.cn,
   module.ps.cn,

[{ for p in sort(keys(var.pm)) :
p => module.cd[p].pd
}]
)

The given value is not suitable for child module variable “pm”
defined at : all list
elements must have the same type.

Hi @gauravachatrath ,
not sure if I understand correctly.
1.) Which is the iterator used for the for_each? You might have to use the proper map key for accessing the module instances.

2.) What type and values does var.pm have?

variable “pm” {
type = map(object({
name = string
description = string
}))
default = {
bm = {
name = “test2”
description = “test”
}
cm = {
name = “test1”
description = “test”
},
}

This is pm.