Terraform Modules and Input Variables
Terraform modules are folders of configuration that you can call from other configurations. A module accepts input variables, creates resources, and returns outputs. Modules keep repeated infrastructure, such as a standard network layout, in one place.
A root configuration calls a local module like this:
hcl module "network" { source = "./modules/network" cidrblock = "10.20.0.0/16" env = var.env }
output "vpcid" { value = module.network.vpcid }
Inside the module, declare inputs with variable blocks and expose results with output blocks. Give each variable a type and a description, and add a validation block where the value must meet a rule.
The usual workflow runs from the root folder:
bash terraform init terraform fmt -recursive terraform validate terraform plan -var="env=staging" -out=tfplan terraform apply tfplan
Saving the plan with -out and applying that exact file makes sure what you reviewed is what runs.
When you change a module source or add a new module, run terraform init -upgrade to refresh the lock file. Pin module versions in registry sources to avoid surprise changes when upstream releases new code.
To keep the plan readable in reviews, put each module call in its own file and run terraform plan from the root folder after every change.