Consume Terraform locals located in another folder

I have a use case in which we need to create local variables that depend heavily on Terraform functions for each environment. We would like to place these local variables within the environment folders and consume them from the root folder where we have the main.tf file that creates certain resources based on these local variables. However, so far, we have not found a way to achieve this as all .tf files need to be located in the same folder.

It is not possible to change these local variables to tfvars as the generation of the values requires the use of functions and loops. Moreover, these values change per environment, which is why we can’t put them in the root folder.

We want to avoid copying and pasting the main.tf file located in the root folder to the environment folder because we want to avoid environment drift.

The structure that we need is:

/-
| main.tf
|variables.tf
|/environment
----/dev
-------| - locals.tf
-------| - terraform.tfvars
----/pre
-------| - locals.tf
-------| - terraform.tfvars
----/pro
-------| - locals.tf
-------| - terraform.tfvars

Is there a way that Terraform can support it? Thank you in advance

Hi @elbatane,

Can you share a little more information about your use case and perhaps an example ‘local’ that you are using between the environments and how its definition changes from one environment to another.

I am struggling to think of why it should be needed to have completely different locals definitions between environments where they cannot be dynamically determined or interpolated based upon input variables or outputs from resource or data blocks. So an example may help me understand and then assist you.

Thanks