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.
How to return any value from the module?
it can be done by using output variables. e.g. in output.tf file:
output “asg_name” {
value = “${aws_auto_scaling_group.asg.name}”
description = “Name of the ASG”
}
How can we consume the output of the module?
The output of the module can be consumed by using "module" reference: module.myModule.asg_name
Can we provide the absolute path of the files?
No, in Terraform we are only allowed to provide the relative path, not “absolute” path.
How Terraform interpret the relative path?
The Terraform always interpret the relative path as per the current working directory. Apart from this, it will start giving error.
Terraform find relative path of the file, but if the file is in separate folder and we are working in another folder, how can we provide the path of the file?
The Terraform interpret path relative to the current working directory.
To solve path problem, Terraform provides different types of path references.
What are different path references and how can we use them?
Different Path references are:
- path.module: It is used when the file is inside the module and root module is different
- path.root or path.cwd: It is used when we need to provide the file path in relative to the root module or cwd.
Write the general syntax of consuming path reference types?
”${path.module/myFolder/myFile}”
What are inline blocks? Is it preferrable?
Inline blocks are the blocks written inside the resource object e.g. ingress and egress
The use of inline block should be avoided, instead, we should be using all resource blocks, to give more flexibility to the user.
How can we create versioning of the Terraform modules?
Since Terraform is just a source code and it doesn’t creates any artifacts, hence its versioning is controlled by “git tags”.