0
0
Terraformcloud~10 mins

Why modules enable reusability in Terraform - Visual Breakdown

Choose your learning style9 modes available
Process Flow - Why modules enable reusability
Write module code once
Save module as reusable unit
Call module multiple times
Each call creates resources
Manage infrastructure easily
Modules let you write code once and use it many times, making infrastructure easier to manage and reuse.
Execution Sample
Terraform
module "network1" {
  source = "./modules/network"
  cidr_block = "10.0.0.0/16"
}

module "network2" {
  source = "./modules/network"
  cidr_block = "10.1.0.0/16"
}
This code calls the same network module twice with different settings to create two networks.
Process Table
StepActionModule CalledInput VariablesResources Created
1Start Terraform apply---
2Call module 'network1' first timenetwork1cidr_block=10.0.0.0/16VPC with 10.0.0.0/16
3Call module 'network2' second timenetwork2cidr_block=10.1.0.0/16VPC with 10.1.0.0/16
4Finish apply--Two separate VPCs created
5ExitNo more modules to call--
💡 All module calls completed, infrastructure created as per inputs
Status Tracker
VariableStartAfter 1After 2Final
cidr_blockundefined10.0.0.0/1610.1.0.0/1610.1.0.0/16
resources_creatednoneVPC with 10.0.0.0/16VPC with 10.1.0.0/16Two VPCs total
Key Moments - 2 Insights
Why can we use the same module multiple times with different inputs?
Because each module call is independent and uses its own input variables, as shown in steps 2 and 3 of the execution_table.
Does writing a module once mean it creates only one resource?
No, each time you call the module it creates resources separately, so multiple calls create multiple resources (see steps 2 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cidr_block input for the second module call?
A10.0.0.0/16
B10.1.0.0/16
Cundefined
D10.2.0.0/16
💡 Hint
Check row 3 under Input Variables in the execution_table
At which step does Terraform finish creating all resources?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the Resources Created column in step 4 of the execution_table
If we change the cidr_block in the first module call, what changes in the variable_tracker?
AFinal value for cidr_block changes to the new first call value
BAfter 2 value for cidr_block changes
COnly After 1 value for cidr_block changes
DNo changes happen
💡 Hint
See how cidr_block changes after each module call in variable_tracker
Concept Snapshot
Modules let you write infrastructure code once and reuse it many times.
Each module call can have different inputs.
Each call creates separate resources.
This saves time and reduces errors.
Modules help organize and scale infrastructure easily.
Full Transcript
Modules in Terraform are reusable units of infrastructure code. You write the module once and then call it multiple times with different inputs. Each call creates its own resources based on the inputs provided. This means you can manage similar infrastructure components easily without repeating code. The execution trace shows two calls to the same network module with different CIDR blocks, resulting in two separate virtual networks. Variables track the inputs and resources created at each step. This approach improves efficiency and organization in cloud infrastructure management.