Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common variables for multiple modules

Tags:

terraform

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...

like image 797
Leo Avatar asked May 25 '26 10:05

Leo


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
}
like image 147
joshduffney Avatar answered May 28 '26 07:05

joshduffney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!