Declare one output to create dynamic list of outputs for multiple modules

I am trying to find a way to declare one output that could loop through multiple module names but am a bit stuck and not sure if it’s possible to achieve this.

I have terraform files with a consistent naming scheme such as <MODULE>.tf and my module has the same name inside that file.

I was able to create a local list of files with the .tf suffix trimmed off so this list contains strings of just <MODULE>

In my output I am trying to set

value = [for i in local.trimmed_file_suffix: module.i.arn]

But get an error message saying that i is not a declared module. I’ve also tried using ${i} and "${i}" in an attempt to get the variable to expand but this gives the same error message about not having a declared module named i.

Is it possible to achieve what I am trying to do?


It is not possible to reference a variable or resource dynamically, other than using the count and for_each mechanisms.

You would need to have the exact module names in the code. You could write some sort of script which creates a .tf file with the correct syntax, and then run that script before running Terraform. Alternatively look at using a single module block with for_each.

1 Like