...

/

Module Variables

Module Variables

Study the different types of Terraform variables in detail.

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:

Press + to interact
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, ...