A child module is a reusable set of Terraform configuration files that can be called from another module or root configuration to organize and share infrastructure code.
Click to reveal answer
beginner
How do you call a child module in Terraform?
You use the module block with a source attribute pointing to the child module's directory or repository.
Click to reveal answer
beginner
Why use child modules in Terraform?
Child modules help organize code, promote reuse, reduce duplication, and make infrastructure easier to manage and maintain.
Click to reveal answer
intermediate
What files are typically included in a Terraform child module?
A child module usually contains main.tf for resources, variables.tf for inputs, outputs.tf for outputs, and optionally versions.tf for provider versions.
Click to reveal answer
intermediate
How do you pass variables to a child module?
You define variables in the child module and pass values to them in the parent module's module block using key-value pairs matching the variable names.
Click to reveal answer
What attribute specifies the location of a child module in Terraform?
Asource
Bpath
Clocation
Dmodule_path
✗ Incorrect
The source attribute tells Terraform where to find the child module's code.
Which file usually contains resource definitions in a child module?
Avariables.tf
Boutputs.tf
Cmain.tf
Dproviders.tf
✗ Incorrect
main.tf typically holds the resource definitions.
How do you reuse infrastructure code in Terraform?
ABy copying and pasting code
BBy creating child modules
CBy using environment variables
DBy running terraform init
✗ Incorrect
Child modules allow you to reuse and organize infrastructure code efficiently.
What is the purpose of variables.tf in a child module?
ATo define input variables for the module
BTo output values from the module
CTo configure providers
DTo store backend configuration
✗ Incorrect
variables.tf defines inputs that the module expects.
Which block do you use in the root module to include a child module?
Aresource
Boutput
Cprovider
Dmodule
✗ Incorrect
The module block is used to call child modules.
Explain how to create and use a child module in Terraform.
Think about folder structure and how to connect modules.
You got /4 concepts.
Describe the benefits of using child modules in Terraform projects.
Consider how modular code helps in software projects.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of creating a child module in Terraform?
easy
A. To avoid using variables in Terraform
B. To break down infrastructure into smaller, reusable pieces
C. To increase the number of resources in the root module
D. To deploy resources only in one cloud provider
Solution
Step 1: Understand module purpose
Child modules help organize infrastructure by splitting it into smaller parts.
Step 2: Identify benefits
This makes code reusable and easier to manage across projects.
Final Answer:
To break down infrastructure into smaller, reusable pieces -> Option B
Quick Check:
Child modules = reusable infrastructure parts [OK]
Hint: Child modules = smaller reusable parts of infrastructure [OK]
Common Mistakes:
Thinking child modules increase resource count in root
Believing child modules remove need for variables
Assuming child modules limit to one cloud provider
2. Which Terraform block correctly includes a child module located in the folder ./modules/network?
easy
A. variable "network" { source = "./modules/network" }
B. resource "network" { source = "./modules/network" }
C. provider "network" { source = "./modules/network" }
D. module "network" { source = "./modules/network" }
Solution
Step 1: Identify correct block type
Child modules are included using the module block in Terraform.
Step 2: Check syntax for source
The source attribute points to the child module folder path.
Final Answer:
module "network" { source = "./modules/network" } -> Option D
Quick Check:
Use module block with source path [OK]
Hint: Use module block with source path to include child module [OK]
Common Mistakes:
Using resource or provider blocks instead of module
But Terraform shows an error: An argument named "db_name" is not expected here. Did you mean to define a variable named "db_name"? What is the likely cause?
medium
A. Terraform does not support passing variables to modules
B. The source path is incorrect
C. The child module does not declare a variable named db_name
D. The root module must use resource block instead of module
Solution
Step 1: Analyze error message
The error indicates that the argument "db_name" is not expected, which happens when the child module does not declare a variable named "db_name".
Step 2: Check child module variables
If child module does not declare variable "db_name", Terraform cannot accept the passed value.
Final Answer:
The child module does not declare a variable named db_name -> Option C
Hint: Declare variables in child module to accept passed values [OK]
Common Mistakes:
Ignoring variable declaration in child module
Assuming source path error without checking variables
Thinking modules can't receive variables
5. You want to create a child module that provisions an AWS S3 bucket with a customizable name and versioning enabled by default. Which of the following variables.tf and main.tf snippets inside the child module is the best practice?
hard
A. variables.tf:
variable "bucket_name" { type = string }
variable "versioning" { type = bool, default = true }