What is the difference between a root module and a child module in Terraform?
Understand the difference between root and child modules in Terraform, how module blocks, inputs and outputs work, with examples and interview questions.
Expected Interview Answer
The root module is the working directory where you run Terraform commands and where configuration begins; a child module is any reusable set of Terraform files that the root (or another module) calls with a module block.
Every Terraform configuration has exactly one root module — the .tf files in the directory where you run terraform init and apply. A child module is a self-contained package of resources defined elsewhere (a subfolder, the Registry, or a Git repo) that you invoke and parameterize through input variables, and read results back from through output values. This nesting lets you write a resource pattern once and reuse it across many environments.
- Encourages reuse of proven infrastructure patterns
- Keeps the root configuration small and readable
- Isolates and parameterizes complexity behind inputs and outputs
- Enables consistent environments (dev, staging, prod)
- Makes testing and versioning of infrastructure easier
AI Mentor Explanation
The root module is the team captain on the field who actually starts play and answers to the umpire, while child modules are specialist units like the bowling attack or the batting order. The captain calls on each unit, tells it the match situation as inputs, and receives runs or wickets back as outputs, but only the captain owns the overall game plan executed on the ground.
Step-by-Step Explanation
Step 1
Identify the root
The directory where you run terraform init and apply is always the root module — it is the entry point of the whole configuration.
Step 2
Extract reusable resources
Move a repeatable set of resources (like a VPC or an S3 bucket pattern) into its own folder to become a child module.
Step 3
Define the interface
Declare input variables the child accepts and output values it returns so callers can parameterize and read results.
Step 4
Call the child
In the root, add a module block with a source path and pass values to its inputs.
Step 5
Wire outputs
Reference the child's outputs in the root (module.name.output) to connect modules together.
What Interviewer Expects
- Knowing every configuration has exactly one root module
- Understanding that child modules are called via a module block
- Explaining inputs (variables) and outputs as the module interface
- Recognizing sources: local paths, the Registry, and Git
- Why modules improve reuse and maintainability
Common Mistakes
- Thinking a project can have multiple root modules at once
- Confusing a module with the state file or workspace
- Forgetting that outputs are needed to pass data out of a child
- Believing modules run terraform commands on their own
- Overusing tiny modules that add indirection without value
Best Answer (HR Friendly)
“The root module is simply the main folder where you actually run Terraform, and child modules are reusable building blocks that folder pulls in. It is like a master recipe that calls smaller sub-recipes, so you write a pattern once and reuse it everywhere.”
Code Example
# root main.tf (the directory you run apply in)
module "web_bucket" {
source = "./modules/s3-bucket"
bucket_name = "skillveris-web-assets"
versioning = true
}
output "bucket_arn" {
value = module.web_bucket.arn
}variable "bucket_name" {
type = string
}
variable "versioning" {
type = bool
default = false
}
resource "aws_s3_bucket" "this" {
bucket = var.bucket_name
}
output "arn" {
value = aws_s3_bucket.this.arn
}Follow-up Questions
- How do you pass data from one child module to another?
- What are the valid source types for a module?
- How do you version a module from the Terraform Registry?
- What is the difference between a module and a workspace?
- When would you decide NOT to extract a module?
MCQ Practice
1. How many root modules does a single Terraform configuration have?
The directory where Terraform commands run is the single root module of any configuration.
2. How does a root module invoke a child module?
A module block with a source argument calls a child module and passes it inputs.
3. How does a child module expose data back to its caller?
Output values are the mechanism a child module uses to return data to the calling module.
Flash Cards
What is the root module? — The directory where you run terraform init/apply — the single entry point of the configuration.
What is a child module? — A reusable package of Terraform resources called from another module via a module block.
How do you pass data into a child module? — Through input variables set in the calling module block.
How do you get data out of a child module? — Through output values, referenced as module.<name>.<output>.