0
0
Terraformcloud~10 mins

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

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