Terraform provider/variable sharing in modules
Right now it's not possible to achieve that. There were previous discussions on github about the same topic in the following issues:
- https://github.com/hashicorp/terraform/issues/5480
- https://github.com/hashicorp/terraform/issues/4585
- https://github.com/hashicorp/terraform/issues/2714
- https://github.com/hashicorp/terraform/issues/1742
- https://github.com/hashicorp/terraform/issues/1478
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.
You can abstract provider parameters away from a module by passing in a provider alias to use. This allows you to create a module without reference to things like Region and then pass those details in on invocation.
For your use case, you can define aliased providers in your stack folders (probably best to define this in a file and make symbolic links for each stack folder):
# stacks/{staging,production}/providers.tf
provider "aws" {
alias = "us-east-1"
region = "us-east-1"
}
provider "aws" {
alias = "us-east-2"
region = "us-east-2"
}
Then when you invoke the modules, pass in the provider alias you want to use (this assumes a module uses only 1 of any particular provider type):
# stacks/{staging,production}/main.tf
module "VPC-us-east-1" {
source = "../../modules/VPC"
providers = {
aws = "aws.us-east-1"
}
}
module "VPC-us-east-2" {
source = "../../modules/VPC"
providers = {
aws = "aws.us-east-2"
}
}