Variables for Modules

Hi,

I’m new to Terraform and I’m trying to create a more organized folder structure, but for now I need to replicate all variables files, contents and reference it into modules. There is a way to use a common variables in root folder for all other modules and have specific variables.tf and terraform.tfvars inside each module?

For example:

iac-azure
±main.tf
±providers.tf
±outputs.tf
±resource_group.tf
±variables.tf
+terraform.tfvars
±-- Virtual Network
±vnet.tf
±variables.tf
±outputs.tf
±terraform.tfvars

In this example, I want to each module have the information from root about resource group created and inside vnet module the variables to create virtual network.

Or I missed some guidance about this?

Thanks

If I understand your question right, you’re trying to pass variables set in ROOT variable file, to modules in a sub directory, in this case Virtual Network.

If so, you must first create the variable block in root variable file:
variable “testvar” {
type = “string”
default = “helloworld”
}

Then declare an empty variable in your Virtual NetworkN/variables.tf

For example:
variable “test” {}

Then, you must pass the variable value from root to this module variable in main.tf as:

module “virtualnetwork” {
source = “./Virtual Network”
test = var.testvar
}

shayanmalik4, thank you, got it… I guess my error was applying variable type and value on both variables.tf files.

And even using modules I can link resources, right? Like an storage account module to a vm module: azurerm_storage_account.mystorageaccount.primary_blob_endpoint

Or I need to change the reference about it?

Thanks

1 Like

Yup, you can link resources within modules. And by link I’m assuming “refer to a resource created in module A, in module B”.

I’ve never written any TF for Microsoft envs so I don’t really know what the module that you referred to is. In any case, you would need to make use of “outputs” to output value of one module to then be called in another module. For example let’s say you have moduleA:

moduleA.tf:

resource "azurerm_storage_account" "mystorageaccount" {
....
}
output "endpoint" {
value = azurerm_storage_account.mystorageaccount.primary_blob_endpoint
}

Then you’d have module B:

moduleB.tf:

resource "anotherMSresource" "nameofthisresource" {
SomeValueThatRequiresModA-Endpoint = var.endpoint
}

You would have an empty variable declared in ModB variables.tf file:
variable “endpoint” {}

You will then pass the value of the output you set in ModA to the empty variable you put in ModB, all in the main.tf file:

main.tf:

module "modA" {
source = "<pathToModule>"
}

module "modB" {
source = "<pathToModule>"
endpoint = module.modA.endpoint

It can definitely get a little confusing at the start but you’ll get the hang of it as you practice more.
In summary, create an output block in the (let’s call it primary module) module you want a value to be referred to in other modules (let’s call it secondary).
Create empty variables in (seconday) modules that you want the output value from primary module.
In main.tf, set the empty variable value for secondary module as the output value from primary module.