Terraform provider/variable sharing in modules

a14m picture a14m · Jul 6, 2018 · Viewed 13.7k times · Source

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)

Answer

a14m picture a14m · Jul 7, 2018

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.