In my opinion it does not make sense, why can’t the module simply inherit the required_providers config from the parent? Whit this approach we need to maintain provider versions in many places which makes it less than ideal…
Hi @ntrp,
The required_providers
block declares both the providers a module needs and the local name you tend to refer to each of them.
If provider requirements were automatically inherited from a caller then a module could mean something completely different depending on which module called it, thus making the module incompletely specified.
While you do need to declare the names and source locations of the providers each module uses, you do not necessarily need to declare the version constraints in every module if you don’t want to. Version constraints are not inherited in a parent-child way, but Terraform must find a single version of each provider that all of the modules are compatible with, and so a module that makes no statements about what versions it is compatible with will end up still being bound by any of the other constraints elsewhere in the configuration.
Of course, leaving the version constraints undefined means that there is no statement in the module about which versions of the provider that module is compatible with, and so anyone using that module will have to figure that out for themselves by studying the source code and then write a suitable version constraint in their own calling module, so I would typically recommend specifying at least the minimum version constraint you’ve tested each module with in order to help other future users of the module avoid selecting an incompatible provider version.
Note that it’s expected for different modules to have different version constraints, because each module uses a different subset of a provider’s features: if one module only uses provider features that have been available since version 1.0, there’s no harm in it declaring version = ">= 1.0.0"
even if some other module in your configuration happens to rely on much newer features of the same provider and therefore declares version = "> 5.0.0"
. Terraform would conclude that the overall configuration requires at least version 5.0.0, because that is the set of versions that all of the modules declare they are compatible with.
With all of that said then: required_providers
is a per-module block because the information it contains is module-specific. There is no need to try to make all of your modules have completely identical required_providers
blocks; as long as they all declare the providers and provider versions they specifically need to do their work, Terraform will take all of that information together in order to decide the full set of providers to install.