Terraform Modules Deep Dive Cheat Sheet
Detailed reference for structuring, calling, and versioning reusable Terraform modules including inputs, outputs, and remote sources.
2 PagesAdvancedFeb 8, 2026
Standard Module Layout
Conventional file layout for a reusable Terraform module.
bash
modules/vpc/├── main.tf # Resource definitions├── variables.tf # Input variable declarations├── outputs.tf # Output value declarations├── versions.tf # required_providers / required_version└── README.md # Usage docs
Variables & Outputs
Declaring typed inputs and exposing outputs from a module.
hcl
# variables.tfvariable "cidr_block" { type = string description = "CIDR range for the VPC" default = "10.0.0.0/16"}variable "subnet_count" { type = number default = 2}# outputs.tfoutput "vpc_id" { value = aws_vpc.this.id description = "ID of the created VPC"}
Calling a Module
Referencing local and remote (registry/Git) module sources.
hcl
module "vpc" { source = "./modules/vpc" # Local path cidr_block = "10.1.0.0/16" subnet_count = 3}module "vpc_registry" { source = "terraform-aws-modules/vpc/aws" # Registry version = "~> 5.0" cidr = "10.2.0.0/16"}module "vpc_git" { source = "git::https://github.com/org/tf-modules.git//vpc?ref=v1.4.0"}# Reference an output from another moduleresource "aws_instance" "app" { subnet_id = module.vpc.public_subnet_ids[0]}
Module Composition Features
Meta-arguments and patterns for flexible, reusable modules.
- count / for_each on module blocks- Instantiate a module multiple times, e.g. for_each = toset(var.environments)
- depends_on (module)- Force explicit ordering when dependencies aren't inferable from references
- providers = { aws = aws.west }- Pass specific provider configurations/aliases into a child module
- terraform_remote_state- Data source to read outputs from another Terraform state/module
- locals block- Compute derived values once and reuse them across a module for DRY expressions
- moved block- Declares a resource/module was renamed or relocated, avoiding destroy/recreate on refactors
Module Versioning & Sources
Pinning module versions for reproducible infrastructure.
- version = "~> 5.0"- Registry modules support version constraints, resolved via terraform init
- ?ref=v1.4.0- Git sources pin to a tag, branch, or commit SHA
- terraform init -upgrade- Re-resolve modules/providers to the latest versions matching constraints
- .terraform.lock.hcl- Lock file recording exact provider versions and checksums (not module versions)
Pro Tip
Pin remote module sources to an immutable ref (a Git tag or commit SHA, or a registry version constraint) rather than a branch like `main` — otherwise a plan can silently pick up unreviewed upstream changes.
Was this cheat sheet helpful?
Explore Topics
#TerraformModulesDeepDive#TerraformModulesDeepDiveCheatSheet#DevOps#Advanced#StandardModuleLayout#VariablesOutputs#CallingAModule#ModuleCompositionFeatures#InfrastructureAsCode#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance