Bird
Raised Fist0
Terraformcloud~10 mins

Creating a child module in Terraform - Visual Walkthrough

Choose your learning style10 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.

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

  1. Step 1: Understand module purpose

    Child modules help organize infrastructure by splitting it into smaller parts.
  2. Step 2: Identify benefits

    This makes code reusable and easier to manage across projects.
  3. Final Answer:

    To break down infrastructure into smaller, reusable pieces -> Option B
  4. 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

  1. Step 1: Identify correct block type

    Child modules are included using the module block in Terraform.
  2. Step 2: Check syntax for source

    The source attribute points to the child module folder path.
  3. Final Answer:

    module "network" { source = "./modules/network" } -> Option D
  4. 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
  • Placing source inside resource or variable blocks
  • Incorrect block type for including modules
3. Given this root module code:
module "web" {
  source = "./modules/webserver"
  instance_count = 3
}

And the child module variables.tf contains:
variable "instance_count" {
  type = number
}

What will be the value of var.instance_count inside the child module?
medium
A. 3
B. 0
C. null
D. 1

Solution

  1. Step 1: Check variable passing

    The root module passes instance_count = 3 to the child module.
  2. Step 2: Confirm variable usage

    The child module declares instance_count variable to receive this value.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Passed variable value = 3 [OK]
Hint: Passed variable values appear inside child module as given [OK]
Common Mistakes:
  • Assuming default or null if variable is declared
  • Confusing variable names or missing passing
  • Thinking child module variables reset automatically
4. You wrote this root module code:
module "db" {
  source = "./modules/database"
  db_name = "prod"
}

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

  1. 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".
  2. Step 2: Check child module variables

    If child module does not declare variable "db_name", Terraform cannot accept the passed value.
  3. Final Answer:

    The child module does not declare a variable named db_name -> Option C
  4. Quick Check:

    Passing undeclared argument causes "not expected" error [OK]
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 }

main.tf:
resource "aws_s3_bucket" "bucket" {
  bucket = var.bucket_name
  versioning {
    enabled = var.versioning
  }
}
B. variables.tf:
variable "bucket_name" { type = string, default = "mybucket" }
variable "versioning" { type = string }

main.tf:
resource "aws_s3_bucket" "bucket" {
  bucket = var.bucket_name
  versioning {
    enabled = var.versioning
  }
}
C. variables.tf:
variable "bucket_name" { type = string }
variable "versioning" { type = string, default = "true" }

main.tf:
resource "aws_s3_bucket" "bucket" {
  bucket = var.bucket_name
  versioning {
    enabled = var.versioning
  }
}
D. variables.tf:
variable "bucket_name" { type = number }
variable "versioning" { type = bool }

main.tf:
resource "aws_s3_bucket" "bucket" {
  bucket = var.bucket_name
  versioning {
    enabled = var.versioning
  }
}

Solution

  1. Step 1: Check variable types and defaults

    Bucket name should be string without default to force user input. Versioning is boolean with default true.
  2. Step 2: Validate resource usage

    Resource uses var.bucket_name and var.versioning correctly with boolean type for versioning enabled.
  3. Final Answer:

    variables.tf with string and bool (default true), main.tf using these variables correctly -> Option A
  4. Quick Check:

    Correct types and defaults = variables.tf:
    variable "bucket_name" { type = string }
    variable "versioning" { type = bool, default = true }

    main.tf:
    resource "aws_s3_bucket" "bucket" {
      bucket = var.bucket_name
      versioning {
        enabled = var.versioning
      }
    }
    [OK]
Hint: Use correct variable types and defaults for best practice [OK]
Common Mistakes:
  • Using string type for boolean variables
  • Setting wrong default types
  • Using number type for bucket name
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard