Bird
Raised Fist0
Terraformcloud~10 mins

Module composition patterns in Terraform - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Module composition patterns
Define Root Module
↓
Call Child Module A
→Child Module A Resources
↓
Call Child Module B
→Child Module B Resources
↓
Pass Outputs Between Modules
↓
Apply Configuration
This flow shows how a root Terraform module calls child modules, which create resources, and how outputs can be passed between them before applying the configuration.
Execution Sample
Terraform
module "network" {
  source = "./modules/network"
  cidr_block = "10.0.0.0/16"
}

module "servers" {
  source = "./modules/servers"
  network_id = module.network.vpc_id
}
This Terraform code composes two modules: 'network' creates a VPC, and 'servers' creates servers inside that VPC using the network's output.
Process Table
StepActionModuleInput/OutputResult
1Start root modulerootNo inputs yetReady to call child modules
2Call module 'network'networkcidr_block=10.0.0.0/16Creates VPC resource
3Output from 'network'networkvpc_id=vpc-12345VPC ID available
4Call module 'servers'serversnetwork_id=vpc-12345Creates servers in VPC
5Apply all resourcesrootAll modules appliedInfrastructure created
6EndrootAll doneExecution complete
💡 All modules called and resources created, execution ends.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
cidr_blockundefined10.0.0.0/1610.0.0.0/1610.0.0.0/1610.0.0.0/16
vpc_idundefinedundefinedvpc-12345vpc-12345vpc-12345
network_idundefinedundefinedundefinedvpc-12345vpc-12345
Key Moments - 3 Insights
Why does the 'servers' module use 'module.network.vpc_id' as input?
Because 'vpc_id' is an output from the 'network' module created earlier (see Step 3 in execution_table), it provides the ID of the VPC needed by 'servers' to place resources inside the network.
What happens if the 'network' module fails to create the VPC?
The 'servers' module cannot get a valid 'vpc_id' output (Step 3), so it will not have the required input to create servers, causing the overall apply to fail.
Can modules be called multiple times with different inputs?
Yes, each module call is independent with its own inputs, allowing reuse of the same module code to create multiple similar resource sets.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'vpc_id' after Step 3?
Aundefined
Bvpc-12345
C10.0.0.0/16
Dmodule.network
💡 Hint
Check the 'Output from network' row in execution_table at Step 3.
At which step does the 'servers' module receive its input?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for when 'servers' module is called with 'network_id' input in execution_table.
If the 'network' module output changed, how would that affect the variable_tracker?
A'cidr_block' would change after Step 2
B'network_id' would be undefined throughout
C'vpc_id' values would update after Step 3 and Step 4
DNo variables would change
💡 Hint
Outputs from 'network' module affect 'vpc_id' and 'network_id' variables in variable_tracker.
Concept Snapshot
Terraform modules let you organize infrastructure code.
Root module calls child modules with inputs.
Child modules create resources and provide outputs.
Outputs can be inputs to other modules.
This pattern helps reuse and organize infrastructure cleanly.
Full Transcript
This visual execution shows how Terraform composes modules. The root module calls a 'network' module with a CIDR block input. The 'network' module creates a VPC and outputs its ID. Then the root calls a 'servers' module, passing the VPC ID as input. The 'servers' module creates servers inside that VPC. Variables like 'vpc_id' and 'network_id' track values passed between modules. The execution table shows each step: starting root, calling modules, outputs, and applying resources. Key moments clarify why outputs are passed and what happens if a module fails. The quiz tests understanding of variable values and module input timing. The snapshot summarizes module composition as a way to organize and reuse infrastructure code by passing inputs and outputs between modules.

Practice

(1/5)
1. What is the main purpose of using module composition patterns in Terraform?
easy
A. To combine multiple modules by passing outputs as inputs for better organization
B. To write all resources in a single large file
C. To avoid using variables in Terraform configurations
D. To deploy resources without any dependencies

Solution

  1. Step 1: Understand module composition

    Module composition means connecting modules by using outputs from one as inputs to another.
  2. Step 2: Recognize benefits

    This approach keeps infrastructure code organized, reusable, and easier to manage.
  3. Final Answer:

    To combine multiple modules by passing outputs as inputs for better organization -> Option A
  4. Quick Check:

    Module composition = combining modules with outputs as inputs [OK]
Hint: Think of modules as building blocks connected by outputs [OK]
Common Mistakes:
  • Thinking modules should be combined by copying code
  • Believing variables are avoided in module composition
  • Assuming modules work independently without connections
2. Which of the following is the correct syntax to pass an output from one module as an input to another in Terraform?
easy
A. output.moduleA.output_name
B. module.moduleA.output_name
C. var.moduleA.output_name
D. resource.moduleA.output_name

Solution

  1. Step 1: Recall Terraform module output reference syntax

    Outputs from a module are accessed using module.<module_name>.<output_name>.
  2. Step 2: Compare options

    Only module.moduleA.output_name matches the correct syntax for accessing module outputs.
  3. Final Answer:

    module.moduleA.output_name -> Option B
  4. Quick Check:

    Module output reference = module.module_name.output_name [OK]
Hint: Outputs are accessed via module.<name>.<output> [OK]
Common Mistakes:
  • Using var or resource instead of module to access outputs
  • Confusing output blocks with module references
  • Using incorrect keywords like output.moduleA
3. Given the following Terraform code snippet, what will be the value of moduleB's input instance_id?
module "moduleA" {
  source = "./moduleA"
}

module "moduleB" {
  source      = "./moduleB"
  instance_id = module.moduleA.instance_id
}
medium
A. The value of the output named instance_id from moduleA
B. A syntax error because instance_id is not defined
C. The string literal 'module.moduleA.instance_id'
D. Null because moduleB does not have instance_id output

Solution

  1. Step 1: Identify module output usage

    The input instance_id for moduleB is assigned from module.moduleA.instance_id, which means it uses the output named instance_id from moduleA.
  2. Step 2: Understand value passing

    This passes the actual value of moduleA's output to moduleB's input, not a string or error.
  3. Final Answer:

    The value of the output named instance_id from moduleA -> Option A
  4. Quick Check:

    moduleB.instance_id = moduleA.instance_id output value [OK]
Hint: module.<name>.<output> passes actual output value, not string [OK]
Common Mistakes:
  • Thinking it passes a string literal instead of value
  • Assuming syntax error due to missing variable declaration
  • Confusing inputs and outputs between modules
4. You have two modules: moduleA outputs vpc_id, and moduleB requires vpc_id as input. The following code causes an error:
module "moduleA" {
  source = "./moduleA"
}

module "moduleB" {
  source = "./moduleB"
  vpc_id = var.vpc_id
}
What is the most likely cause of the error?
medium
A. moduleB source path is incorrect
B. moduleA does not have an output named vpc_id
C. moduleB is using var.vpc_id instead of module.moduleA.vpc_id as input
D. Terraform does not support passing outputs between modules

Solution

  1. Step 1: Check input assignment in moduleB

    moduleB expects vpc_id input, but it is assigned from var.vpc_id, which is a variable, not the output from moduleA.
  2. Step 2: Identify correct way to pass output

    The correct way is to assign vpc_id = module.moduleA.vpc_id to pass the output from moduleA.
  3. Final Answer:

    moduleB is using var.vpc_id instead of module.moduleA.vpc_id as input -> Option C
  4. Quick Check:

    Pass outputs via module.moduleA.output, not var [OK]
Hint: Use module.<name>.<output> to pass outputs, not var.<name> [OK]
Common Mistakes:
  • Assuming var.vpc_id automatically links to moduleA output
  • Ignoring the need to explicitly connect modules
  • Thinking Terraform cannot pass outputs between modules
5. You want to create a composed Terraform setup where moduleC depends on outputs from both moduleA and moduleB. Which pattern correctly composes these modules to pass outputs as inputs to moduleC?
// Option A
module "moduleC" {
  source = "./moduleC"
  inputA = module.moduleA.outputA
  inputB = module.moduleB.outputB
}

// Option B
module "moduleC" {
  source = "./moduleC"
  inputA = var.outputA
  inputB = var.outputB
}

// Option C
module "moduleC" {
  source = "./moduleC"
  inputA = module.moduleB.outputB
  inputB = module.moduleA.outputA
}

// Option D
module "moduleC" {
  source = "./moduleC"
  inputA = module.moduleA.outputB
  inputB = module.moduleB.outputA
}
hard
A. Mix outputs incorrectly between modules
B. Use variables instead of module outputs for inputs
C. Swap inputs but still use correct module outputs
D. Pass outputs correctly matching input names from moduleA and moduleB

Solution

  1. Step 1: Match inputs to correct module outputs

    Pass outputs correctly matching input names from moduleA and moduleB passes inputA from moduleA.outputA and inputB from moduleB.outputB, matching names correctly.
  2. Step 2: Evaluate other options

    Use variables instead of module outputs for inputs uses variables instead of outputs, which breaks composition. Swap inputs but still use correct module outputs swaps the assignments, mismatching inputA (expects moduleA.outputA) with moduleB.outputB and inputB with moduleA.outputA. Mix outputs incorrectly between modules mixes outputs incorrectly, likely causing errors.
  3. Final Answer:

    Pass outputs correctly matching input names from moduleA and moduleB -> Option D
  4. Quick Check:

    Correct output-to-input mapping = Pass outputs correctly matching input names from moduleA and moduleB [OK]
Hint: Match input names to correct module outputs carefully [OK]
Common Mistakes:
  • Swapping outputs between modules without checking input expectations
  • Using variables instead of module outputs
  • Mixing output names incorrectly causing runtime errors
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