Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform provider/variable sharing in modules

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)

like image 377
a14m Avatar asked Jul 06 '18 15:07

a14m


People also ask

How do I share variables between modules in Terraform?

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.

How do I pass a provider in module Terraform?

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.

Can Terraform variables reference other variables?

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.

Can Terraform modules use other modules?

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.


1 Answers

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.

like image 110
a14m Avatar answered Sep 28 '22 01:09

a14m