What if you could build your cloud setup like snapping Lego blocks together, making changes without fear?
Why Module composition patterns in Terraform? - Purpose & Use Cases
Imagine you are building a cloud setup by writing all the infrastructure code in one big file. Every time you want to add a new feature, you have to scroll through hundreds of lines, copy-paste, and carefully change values.
This feels like trying to build a complex Lego model without sorting the pieces or instructions.
Doing everything manually in one place is slow and risky. A small mistake can break the whole setup. It's hard to reuse parts, and updating one piece means touching many others.
You waste time fixing errors and lose confidence in your work.
Module composition patterns let you break your infrastructure into smaller, reusable building blocks called modules. You can combine these blocks like Lego sets to build complex systems easily.
This makes your code cleaner, safer, and faster to change.
resource "aws_instance" "web" { ami = "ami-123" instance_type = "t2.micro" } resource "aws_instance" "db" { ami = "ami-456" instance_type = "t2.medium" }
module "web_server" { source = "./modules/web" instance_type = "t2.micro" } module "database" { source = "./modules/db" instance_type = "t2.medium" }
You can build, update, and share complex cloud setups quickly and confidently by composing simple modules.
A company uses module composition to create a standard network module, a security module, and a compute module. Teams combine these modules to launch new projects without rewriting code, saving weeks of work.
Manual infrastructure code is hard to manage and error-prone.
Module composition breaks code into reusable, manageable parts.
This approach speeds up development and reduces mistakes.
Practice
Solution
Step 1: Understand module composition
Module composition means connecting modules by using outputs from one as inputs to another.Step 2: Recognize benefits
This approach keeps infrastructure code organized, reusable, and easier to manage.Final Answer:
To combine multiple modules by passing outputs as inputs for better organization -> Option AQuick Check:
Module composition = combining modules with outputs as inputs [OK]
- Thinking modules should be combined by copying code
- Believing variables are avoided in module composition
- Assuming modules work independently without connections
Solution
Step 1: Recall Terraform module output reference syntax
Outputs from a module are accessed usingmodule.<module_name>.<output_name>.Step 2: Compare options
Only module.moduleA.output_name matches the correct syntax for accessing module outputs.Final Answer:
module.moduleA.output_name -> Option BQuick Check:
Module output reference = module.module_name.output_name [OK]
- Using var or resource instead of module to access outputs
- Confusing output blocks with module references
- Using incorrect keywords like output.moduleA
moduleB's input instance_id?
module "moduleA" {
source = "./moduleA"
}
module "moduleB" {
source = "./moduleB"
instance_id = module.moduleA.instance_id
}Solution
Step 1: Identify module output usage
The inputinstance_idfor moduleB is assigned frommodule.moduleA.instance_id, which means it uses the output namedinstance_idfrom moduleA.Step 2: Understand value passing
This passes the actual value of moduleA's output to moduleB's input, not a string or error.Final Answer:
The value of the output named instance_id from moduleA -> Option AQuick Check:
moduleB.instance_id = moduleA.instance_id output value [OK]
- Thinking it passes a string literal instead of value
- Assuming syntax error due to missing variable declaration
- Confusing inputs and outputs between 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?Solution
Step 1: Check input assignment in moduleB
moduleB expectsvpc_idinput, but it is assigned fromvar.vpc_id, which is a variable, not the output from moduleA.Step 2: Identify correct way to pass output
The correct way is to assignvpc_id = module.moduleA.vpc_idto pass the output from moduleA.Final Answer:
moduleB is using var.vpc_id instead of module.moduleA.vpc_id as input -> Option CQuick Check:
Pass outputs via module.moduleA.output, not var [OK]
- Assuming var.vpc_id automatically links to moduleA output
- Ignoring the need to explicitly connect modules
- Thinking Terraform cannot pass outputs between modules
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
}Solution
Step 1: Match inputs to correct module outputs
Pass outputs correctly matching input names from moduleA and moduleB passesinputAfrommoduleA.outputAandinputBfrommoduleB.outputB, matching names correctly.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.Final Answer:
Pass outputs correctly matching input names from moduleA and moduleB -> Option DQuick Check:
Correct output-to-input mapping = Pass outputs correctly matching input names from moduleA and moduleB [OK]
- Swapping outputs between modules without checking input expectations
- Using variables instead of module outputs
- Mixing output names incorrectly causing runtime errors
Ready to prove it?
Pass the test and this pattern moves unknown
