0
0
GCPcloud~10 mins

Modules for reusability in GCP - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Modules for reusability
Create Module Files
Define Resources & Variables
Use Module in Main Config
Pass Inputs to Module
Terraform Init & Apply
Module Creates Resources
Reuse Module Anywhere
END
This flow shows how you create a module, define its resources, use it in your main config, pass inputs, and deploy reusable infrastructure.
Execution Sample
GCP
module "vm_instance" {
  source = "./modules/vm"
  name   = "my-vm"
  zone   = "us-central1-a"
}
This code uses a module to create a VM instance with reusable parameters for name and zone.
Process Table
StepActionInput/VariableResult/State Change
1Create module directory and filesmodules/vm/main.tf, variables.tfModule files ready with resource and variables defined
2Define variables in modulevar.name, var.zoneVariables declared for module inputs
3Write resource using variablesgoogle_compute_instance with var.name and var.zoneResource template ready for reuse
4In main config, call module with source and inputssource=./modules/vm, name=my-vm, zone=us-central1-aModule block added to main config
5Run terraform initInitialize modulesModules downloaded and initialized
6Run terraform applyApply configVM instance created with name=my-vm in zone=us-central1-a
7Reuse module with different inputsname=your-vm, zone=us-east1-bAnother VM created with new inputs
8ExitNo more modules to applyInfrastructure deployed with reusable modules
💡 All modules applied and resources created; no more modules to process.
Status Tracker
VariableStartAfter Step 4After Step 6After Step 7Final
var.nameundefinedmy-vmmy-vmyour-vmyour-vm
var.zoneundefinedus-central1-aus-central1-aus-east1-bus-east1-b
Key Moments - 3 Insights
Why do we define variables inside the module instead of hardcoding values?
Variables allow the module to accept different inputs each time it is used, making it reusable with different configurations as shown in steps 2 and 7.
What happens if we forget to run 'terraform init' after adding a module?
'terraform init' downloads and initializes modules. Without it, Terraform cannot use the module source, so apply will fail (see step 5).
Can we use the same module multiple times with different inputs?
Yes, as shown in step 7, reusing the module with different inputs creates separate resources without rewriting code.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of var.name after step 6?
Amy-vm
Byour-vm
Cundefined
Dus-central1-a
💡 Hint
Check variable_tracker row for var.name at After Step 6 column.
At which step does Terraform initialize the modules?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look at the Action column in execution_table for 'terraform init'.
If you want to create a VM in a new zone, which step shows how to do it?
AStep 4
BStep 7
CStep 2
DStep 6
💡 Hint
Step 7 shows reusing the module with different inputs.
Concept Snapshot
Modules let you package infrastructure code for reuse.
Define variables inside modules for flexibility.
Call modules in main config with inputs.
Run 'terraform init' to prepare modules.
Apply creates resources from module code.
Reuse modules by changing inputs only.
Full Transcript
Modules for reusability in GCP Terraform let you write infrastructure code once and use it many times with different inputs. First, you create module files defining resources and variables. Then, in your main configuration, you call the module and pass values for those variables. Running 'terraform init' downloads and prepares the module. Applying the configuration creates the resources. You can reuse the same module multiple times by passing different inputs, saving time and avoiding repeated code.