hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.34k stars 9.49k forks source link

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

Open gchiesa opened 2 years ago

gchiesa commented 2 years ago

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

kmoe commented 2 years ago

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.