Common variables for multiple modules

Viewed 516

Is this the right way to share variables across modules or am I doing it wrong?

In a base module I add a variable:

variable "aws_region" {
  description = "Region to use"
  default     = "eu-west-1"
}

Then I expose it using output:

output "AwsRegion" {
  value = var.aws_region
}

Then in the module I want to use it from, I import the base module and refer to it:

module "base" {
  source = "../base"
}

provider "aws" {
  version = "~> 2.37"
  region  = module.base.AwsRegion
}

Is there a way to define the variable and export it as output in one go? Does Terraform have the concept of access modifiers? It would be great to be able to do public variable or output variable...

1 Answers

I'm not positive it's the best way, but I've been passing them in at the main.tf file. By doing this I can see the new variables being defined as well as the values that are populating the modules' variables.

module "linuxvm" {
  source = "./linuxvm"
  vmprefix = var.prefix
  resourcegroup = azurerm_resource_group.main.name
  location = azurerm_resource_group.main.location
  vmos = var.os
  password = var.vmpassword
  subnetid = module.network.subnetid
}
Related