Is there a way of abstracting the provider for all the modules defined in a project.
for example, I have this project
├── modules │ ├── RDS │ └── VPC └── stacks ├── production │ └── main.tf └── staging └── main.tf
and it works fine... the problem is with the definition of modules
├── RDS │ ├── README.md │ ├── main.tf │ ├── providers.tf │ └── variables.tf └── VPC ├── README.md ├── main.tf ├── providers.tf └── variables.tf
the provider in both of these modules are exactly the same
# providers.tf provider "aws" { region = "${var.region}" version = "~> 1.26" }
and the variables in each module are different but they all have the region
variable.
# variables.tf variable "region" { default = "eu-central-1" description = "AWS region." } # other module dependent variables...
is there a way to define those bits of information on the modules level so that I end up with something roughly like this
├── modules │ ├── providers.tf <<< include the *shared* provider definition block │ ├── variables.tf <<< include the *shared* region vaiable definition block │ ├── RDS │ │ ├── README.md │ │ ├── main.tf │ │ └── variables.tf │ └── VPC │ ├── README.md │ ├── main.tf │ └── variables.tf
one last thing, the modules definitions most of the time have a resource attribute (pulling a module from the terraform registry... therefore I don't know if it's feasible to inherit both the source from the registry and a base module)
Steps: Clone the repo from here. Change your directory to ./terraform/passing-outputs so you can list modules directory, variables.tf and main.tf files. Change the variables in root variables.tf file according to your needs.
Providers can be passed down to descendent modules in two ways: either implicitly through inheritance, or explicitly via the providers argument within a module block.
It is a group of key-value pairs that can be used in the configuration. The values can be hard-coded or be a reference to another variable or resource.
In addition to modules from the local filesystem, Terraform can load modules from a public or private registry. This makes it possible to publish modules for others to use, and to use modules that others have published.
Right now it's not possible to achieve that. There were previous discussions on github about the same topic in the following issues:
TL;DR
the sharing of variables between modules is against terraform core clarity/explicity principles.
Workaround
A workaround is to have the *shared*
files in the parent directory and using symlinks to add them to the modules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With