0
0
Terraformcloud~10 mins

Creating a child module in Terraform - Visual Walkthrough

Choose your learning style9 modes available
Process Flow - Creating a child module
Create child module folder
Write child module files (main.tf, variables.tf)
Reference child module in root module
Run terraform init
Run terraform apply
Child module resources created
This flow shows how to create a child module folder with configuration, reference it in the root module, initialize Terraform, and apply to create resources.
Execution Sample
Terraform
module "child_example" {
  source = "./modules/child"
  name   = "example"
}

# Child module main.tf
resource "null_resource" "example" {
  triggers = { name = var.name }
}

# Child module variables.tf
variable "name" {
  type = string
}
This code defines a child module in a subfolder and calls it from the root module passing a variable.
Process Table
StepActionEvaluationResult
1Create child module folder and filesFiles created: main.tf, variables.tfChild module ready
2Write resource in child module main.tfResource null_resource with trigger nameResource defined
3Reference child module in root modulemodule block with source and name variableModule call ready
4Run terraform initInitialize modules and providersModules and providers initialized
5Run terraform applyApply configurationChild module resource created
6Verify resourceCheck terraform stateResource present in state
7ExitNo more stepsExecution complete
💡 All steps completed, child module created and applied successfully
Status Tracker
VariableStartAfter Step 3After Step 5Final
var.nameundefined"example""example""example"
module.child_exampleundefinedmodule call definedresource createdresource created
Key Moments - 3 Insights
Why do we need to run 'terraform init' after adding a child module?
Because 'terraform init' downloads and initializes modules and providers, making the child module available for use as shown in step 4 of the execution_table.
How does the root module pass variables to the child module?
The root module passes variables by setting arguments inside the module block, like 'name = "example"' in step 3, which the child module accesses via 'var.name'.
What happens if the child module folder or files are missing?
Terraform will fail during 'terraform apply' because it cannot find the module source (local path validated during plan phase), stopping execution at step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the child module resource actually created?
AStep 5
BStep 3
CStep 4
DStep 2
💡 Hint
Refer to step 5 where 'terraform apply' runs and the resource is created.
According to variable_tracker, what is the value of var.name after step 3?
Anull
Bundefined
C"example"
D"child_example"
💡 Hint
Check the 'var.name' row under 'After Step 3' column in variable_tracker.
If the child module folder is missing, which step in execution_table will fail?
AStep 2
BStep 5
CStep 3
DStep 4
💡 Hint
Look at step 5 where 'terraform apply' runs; missing local folder causes failure during module loading in the plan phase.
Concept Snapshot
Creating a child module in Terraform:
- Create a folder with module files (main.tf, variables.tf).
- Define resources inside the child module.
- Reference the child module in root with 'module' block and 'source'.
- Pass variables via module arguments.
- Run 'terraform init' to initialize modules.
- Run 'terraform apply' to create resources.
Full Transcript
To create a child module in Terraform, first make a folder for the module and add configuration files like main.tf and variables.tf. Inside, define the resources you want. Then, in your root module, add a module block that points to the child module folder using the source argument. Pass any needed variables inside this block. Next, run 'terraform init' to download and prepare the module. Finally, run 'terraform apply' to create the resources defined in the child module. This process helps organize infrastructure code into reusable parts.