Terraform Modules Flashcards

1
Q

What is a Terraform module?

A

A module is a “set” of terraform configuration file inside a folder.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s a root module?

A

The set of configuration files inside the current working directory is known as “root” module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Is module a folder or a file?

A

A module is a directory which is nothing but the set of different configuration files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

write the general syntax of using a module

A

module “name_of_module” {
source = “path of the directory”
[config…]
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

how Terraform “senses” changes in source code of any module or addition/removal of any module?

A

Terraform doesn’t have any automated way to sense “changes” in the module. For this it relies on the user’s action.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the first step we should do, whenever we change a module or update same?

A

The first command, that we should run is “terraform init” before any other command like plan or apply.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

In which module should we provide the “backend” configuration?

A

It should always be provided into the root module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What will happen if we “hard-code” resources inside the modules?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What’s the process of externalisation of variables from modules?

A
  1. Create a new file called “variable.tf” in the module folder.
  2. Consume the variable just like normal variable inside the main.tf of module.
  3. Pass-on the variable information from “module” object like an “attribute”.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write the general syntax of passing the variable value to the “module” object.

A
module "my_module" {
   source = "../../my_module_path"
    var_1 = "value"
    var_2 = "value2"
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write general syntax of local variables?

A
locals {
   first = "first"
    second = 34
   third = "third"
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to consume a local variable within the module?

A

By using interpolation syntax with “local” prefix
“${local.first}”
“${local.second}”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is it possible to return anything from a module?

A

yes we can define our module in a way that it can return the value in the form of output variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

can we say terraform module are just like a function?

A

Yes, Terraform module behaves like a function as

  1. They accepts the arguments in the form of variables
  2. They process the information within “isolated” files inside the module folder
  3. They return the information/data in the form of output variables.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to return any value from the module?

A

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”
}

17
Q

How can we consume the output of the module?

A
The output of the module can be consumed by using "module" reference:
module.myModule.asg_name
18
Q

Can we provide the absolute path of the files?

A

No, in Terraform we are only allowed to provide the relative path, not “absolute” path.

19
Q

How Terraform interpret the relative path?

A

The Terraform always interpret the relative path as per the current working directory. Apart from this, it will start giving error.

20
Q

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?

A

The Terraform interpret path relative to the current working directory.
To solve path problem, Terraform provides different types of path references.

21
Q

What are different path references and how can we use them?

A

Different Path references are:

  1. path.module: It is used when the file is inside the module and root module is different
  2. path.root or path.cwd: It is used when we need to provide the file path in relative to the root module or cwd.
22
Q

Write the general syntax of consuming path reference types?

A

”${path.module/myFolder/myFile}”

23
Q

What are inline blocks? Is it preferrable?

A

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.

24
Q

How can we create versioning of the Terraform modules?

A

Since Terraform is just a source code and it doesn’t creates any artifacts, hence its versioning is controlled by “git tags”.

25
Q

Write the general syntax of creating different versions of Terraform modules?

A

git tag -a “v0.0.1” -m “new version”

git push –follow-tags

26
Q

How can we consume a new version of the module in our code?

A

It is consumed by references inside the “source”
module “my_module” {
source = “mypath?ref=v0.0.1”
}

27
Q

What’s the general syntax of importing the existing resource in Terraform module state file?

A

terraform import module… existing_resource_name

28
Q

What’s the general syntax of Terraform import?

A

terraform import

29
Q

My Terraform plan keeps telling me to update the IAM policy, what might be the issue?

A

The provider is changing the content of the policy, which is triggering the “diff”, hence the Terraform plan is asking to revert same to previous state.