terraform icon indicating copy to clipboard operation
terraform copied to clipboard

Ignore providers and backend declarations when code is invoked through `module`

Open gchiesa opened this issue 2 years ago • 1 comments

Current Terraform Version

❯ terraform version
Terraform v1.2.6
on darwin_arm64

Use-cases

Sometimes we produce modules to abstract a set of resources to a certain level that they could be also consumed as root modules.

For a nice developer experience we include in these "root" modules the providers instantiations (they are pretty complex, for example the one for Azurerm) and backend as well (empty configuration only the block), so that they can be used as off-the-shelf solution without having them to wrap once again in a root module.

When we do so, though, the root module itself cannot be used anymore within a parent root module alongside other modules (and with depends_on). In that case terraform will complain about inner module containing providers and backends.

Attempted Solutions

Typically we have to use Terragrunt to orchestrate multiple root modules with dependencies, but sometimes this might be overkill.

Proposal

It would be nice if Terraform is smart enough to ignore providers and backend declarations in the code when this is invoked via a module directive.

References

n/a

gchiesa avatar Aug 05 '22 15:08 gchiesa

Thanks for the issue. Currently, Terraform considers root modules and child modules to be different types of module, and not interchangeable, which is the reason behind the errors you see. Unfortunately at this point it is not possible to remove this error without it being a breaking change in Terraform.

The use case does look reasonable to me, however, so I am leaving this ticket open in case it attracts more discussion.

kmoe avatar Aug 09 '22 14:08 kmoe