Terraform Modules Flashcards
What is a Terraform module?
A module is a “set” of terraform configuration file inside a folder.
What’s a root module?
The set of configuration files inside the current working directory is known as “root” module.
Is module a folder or a file?
A module is a directory which is nothing but the set of different configuration files.
write the general syntax of using a module
module “name_of_module” {
source = “path of the directory”
[config…]
}
how Terraform “senses” changes in source code of any module or addition/removal of any module?
Terraform doesn’t have any automated way to sense “changes” in the module. For this it relies on the user’s action.
What’s the first step we should do, whenever we change a module or update same?
The first command, that we should run is “terraform init” before any other command like plan or apply.
In which module should we provide the “backend” configuration?
It should always be provided into the root module.
What will happen if we “hard-code” resources inside the modules?
In that case, whenever we use that module, we will get the same source name, which may lead to “name” conflicts. Hence we should “externalise” all inputs to the module.
What’s the process of externalisation of variables from modules?
- Create a new file called “variable.tf” in the module folder.
- Consume the variable just like normal variable inside the main.tf of module.
- Pass-on the variable information from “module” object like an “attribute”.
Write the general syntax of passing the variable value to the “module” object.
module "my_module" { source = "../../my_module_path" var_1 = "value" var_2 = "value2" }
How can we restrict the “scope” of a variable within the modules? e.g. we want to create a variable to be used inside the module, but don’t want to expose them to the consumer of the module.
We can restrict the “scope” of the variable inside “module” only by using “local variables”.
The local variables are not allowed to be consumed outside the module.
Write general syntax of local variables?
locals { first = "first" second = 34 third = "third" }
How to consume a local variable within the module?
By using interpolation syntax with “local” prefix
“${local.first}”
“${local.second}”
Is it possible to return anything from a module?
yes we can define our module in a way that it can return the value in the form of output variable.
can we say terraform module are just like a function?
Yes, Terraform module behaves like a function as
- They accepts the arguments in the form of variables
- They process the information within “isolated” files inside the module folder
- They return the information/data in the form of output variables.