Terraform Modules Explained
Learn how Terraform modules help you create reusable, consistent, and maintainable infrastructure code across environments and projects.
What is a Terraform module?
A module is a container for Terraform configuration files. Every Terraform project is a root module. You can also create child modules and reuse them.
Example module structure
modules/ network/ main.tf variables.tf outputs.tf environments/ dev/ main.tf
Calling a module
module "network" { source = "../../modules/network" environment = "dev" vpc_cidr = "10.0.0.0/16" }
Module inputs and outputs
Use variables as module inputs and outputs to expose useful values.
variable "vpc_cidr" { type = string } output "vpc_id" { value = aws_vpc.main.id }
When to create modules
- When the same pattern is repeated.
- When teams need standard infrastructure.
- When environments need consistent setup.
- When code becomes too large in root modules.
Common mistakes
- Creating modules too early for simple projects.
- Building modules with too many inputs.
- Hardcoding environment-specific values inside modules.
- Not versioning shared modules.
Best practices
- Keep modules small and focused.
- Use clear variable names.
- Add outputs only for useful values.
- Document required inputs.
- Version shared modules.
Related guides
- Terraform Learning Path
- Terraform Files Explained
A reusable module with explicit inputs
This is a call-site fragment: it requires an existing modules/network child module with a vpc_cidr variable. Document supported provider versions and outputs, pin remote module sources, and verify CIDR conflicts. Modules reuse infrastructure logic but do not guarantee security.
module "network" {
source = "./modules/network"
vpc_cidr = "10.42.0.0/16"
}Related Melochip article: https://melochip.com/posts/terraform-vpc-network-creation-guide. Examples are illustrative and have not been independently executed.
Practical guidance and verification
Define a reusable contract
A Terraform module groups resources with declared input variables and outputs. Build modules for meaningful reusable responsibilities, not simply to wrap every individual resource. Document required inputs, sensible defaults, supported providers, example usage and which changes can trigger replacement. Keep environment-specific secrets and backend settings in the root module.
Test module changes
Pin module versions or reviewed Git references, then run fmt, validate and plans in disposable environments. Test upgrades as carefully as initial deployment: renaming resource addresses can trigger replacement unless state movements are declared or managed correctly. Avoid deeply nested modules that make dependency and ownership boundaries unclear.
Protect output values
Outputs are interfaces for downstream automation and may be preserved in state. Mark sensitive outputs appropriately but do not assume sensitive flags remove confidential values from the backend. Keep module examples accurate and review published dependencies. See https://developer.hashicorp.com/terraform/language/modules.
Join the discussion
0 CommentsAsk questions, add context, or share what worked.
More from Melochip Editorial
Learn what Playwright testing is, why modern QA teams use it, how testers can get started, and the major types...
8 min read · Updated Nov 16, 2026
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026
Learn Kubernetes security best practices with RBAC, Secrets, ServiceAccounts, Pod securityContext, NetworkPoli...
6 min read · Updated Nov 16, 2026Related articles
Learn how Kubernetes Deployments and ReplicaSets work with practical YAML, scaling, self-healing, rolling upda...
7 min read · Updated Nov 16, 2026
Learn Kubernetes monitoring and logs with kubectl logs, events, metrics-server, top commands, Prometheus, Graf...
6 min read · Updated Nov 16, 2026
Learn Kubernetes Ingress with controllers, IngressClass, host and path routing, TLS, DNS, annotations, redirec...
8 min read · Updated Nov 16, 2026
Learn Kubernetes ConfigMaps and Secrets with YAML, environment variables, file mounts, update behavior, immuta...
8 min read · Updated Nov 16, 2026Stay in the loop. Get new Melochip articles
Receive Melochip article updates when new articles are available. Explicit opt-in only.
No comments yet. Start the discussion.