Search⌘ K

Module Variables

Explore how to use variables within Terraform modules to manage inputs and outputs effectively. Understand the difference between exposed and local variables, overriding variables via the command line, and the significance of the path variable. This lesson provides practical examples to help you create dynamic and reusable Terraform modules.

Variables

Variables in Terraform allow modules to take the input. Since modules are similar to procedures in programming languages, they take the input, return an output, and change the state of resources.

Variables provide the mechanism for inputs to be given and for the outputs to be returned to the consumer of the module. Variables are a critical aspect of Terraform modules.

A simple module variable

We’ve created a directory named ltthw_variable/variable_module and then changed the directory to ltthw_variables/variable_module. Now we’ll create a variable_module.tf file and write the following content in it:

Shell
variable filename {
default = "default_filename.txt"
}
resource "local_file" "hello_local_variable" {
content = "Hello terraform local variable module!"
filename = var.filename
}

We’ve created a module that creates a file with some content statically set within the resource block. What’s new is that we have defined a variable block. This may look like an unusual way of creating a variable as it is quite verbose, but it does have the benefit of making sure that the value default_filename.txt is a default that can be overridden.

Override module variables

To override the module variable, we’ll create a variable_module_consumer directory with the ...