Why modules? DRY, consistent, and production-ready infrastructure
Terraform modules help teams share opinionated patterns—VPCs, EKS clusters, RDS, and more—while keeping configurations DRY, versioned, and reviewable.
Recommended Layout
modules/
vpc/
main.tf
variables.tf
outputs.tf
README.md
examples/
vpc-simple/
main.tf
Keep examples compile-able. Your CI should plan/apply examples in a disposable workspace.
Design Principles
Clear Inputs & Sensible Defaults
Prefer optional() variables with defaults. Validate with validation { condition ... }.
Minimal Outputs
Only expose what downstream stacks truly need (IDs, ARNs, endpoints).
Version Everything
Tag releases and reference modules via immutable versions. Use a private registry or Git source with ref tags.
Automated Checks
Run tflint, terraform fmt, validate, and plan on PRs. Optionally add policy checks (OPA/Sentinel).
Common Gotchas
- Over-generic modules with dozens of flags—prefer focused modules.
- Leaking provider config inside modules—let root define providers.
- Breaking changes without semver—respect
MAJOR.MINOR.PATCH.
SEO Keywords Targeted
terraform modules, aws terraform modules, iac best practices, terraform module versioning, terraform folder structure, reusable terraform, terraform registry private, terraform tflint ci
Key Takeaways
- Keep modules small, composable, and versioned.
- Ship runnable examples and CI checks.
- Document inputs/outputs in
README.md.
FAQs
Should I use Terragrunt? It can simplify multi-env orchestration, but you can also achieve parity with workspaces + pipelines if you prefer pure Terraform.
Where to store modules? Private registry, Git tags, or artifact storage—choose what fits your governance.