Building Terraform Modules
Overview
Build reusable, production-ready Terraform modules with proper variable definitions, outputs, validation rules, documentation, and examples. Generate modules following HashiCorp's standard module structure for AWS, GCP, and Azure resources with security best practices, tagging conventions, and lifecycle management.
Prerequisites
- Terraform 1.0+ installed (
terraform version)
- Cloud provider credentials configured for the target platform
- Understanding of the infrastructure resources the module will manage
- Familiarity with HCL syntax and Terraform module conventions
terraform-docs installed for automated documentation generation (optional)
Instructions
- Define the module scope: determine which cloud resources the module manages and its input/output contract
- Create the standard module file structure:
main.tf, variables.tf, outputs.tf, versions.tf, locals.tf
- Write
versions.tf with required_version and required_providers blocks pinned to stable versions
- Define input variables in
variables.tf with descriptions, types, defaults, and validation rules
- Implement resources in
main.tf using variables for all configurable values; use locals for computed values
- Add meaningful outputs in
outputs.tf for resource IDs, ARNs, endpoints, and connection strings
- Implement security defaults: encryption enabled, public access blocked, least-privilege IAM, logging enabled
- Create an
examples/ directory with at least one complete usage example showing module invocation
- Generate documentation with
terraform-docs markdown table . > README.md
- Validate the module with
terraform init && terraform validate and test with terraform plan using the example
Output
- Module files:
main.tf, variables.tf, outputs.tf, versions.tf, locals.tf
- Example usage in
examples/basic/main.tf with realistic variable values
- Auto-generated
README.md with inputs, outputs, and usage documentation
.terraform-docs.yml configuration for documentation generation
- Optional: test files using Terratest or
terraform test (HCL-based)
Error Handling
| Error |
Cause |
Solution |
variable validation failed |
Input value does not meet validation rule |
Check the validation block in variables.tf; adjust the value or the validation regex |
provider not found |
Missing or wrong provider source in versions.tf |
Add the provider to required_providers with correct source and version constraint |
circular dependency |
Resources referencing each other in a loop |
Refactor to break the cycle; use depends_on or separate into sub-modules |
output references undeclared resource |
Typo in resource name or resource removed |
Verify resource names in main.tf match output references exactly |
module source not found |
Incorrect module path or registry reference |
Verify the source path is relative (e.g., ./modules/vpc) or a valid registry address |
Examples
- "Build a Terraform module for an AWS VPC with configurable CIDR, public/private subnets across 3 AZs, NAT gateway, and flow logs."
- "Create a GCP Cloud Run module with custom domain, IAM bindings, and auto-scaling configuration as input variables."
- "Generate a Terraform module for an S3 bucket with versioning, encryption, lifecycle rules, and access logging, with all settings as optional variables with secure defaults."
Resources
Converted and distributed by TomeVault — claim your Tome and manage your conversions.
1---2name: jeremylongshore-claude-code-plugins-plus-skills-building-ter3description: Building Terraform Modules4---5# Building Terraform Modules67## Overview89Build reusable, production-ready Terraform modules with proper variable definitions, outputs, validation rules, documentation, and examples. Generate modules following HashiCorp's standard module structure for AWS, GCP, and Azure resources with security best practices, tagging conventions, and lifecycle management.1011## Prerequisites1213- Terraform 1.0+ installed (`terraform version`)14- Cloud provider credentials configured for the target platform15- Understanding of the infrastructure resources the module will manage16- Familiarity with HCL syntax and Terraform module conventions17- `terraform-docs` installed for automated documentation generation (optional)1819## Instructions20211. Define the module scope: determine which cloud resources the module manages and its input/output contract222. Create the standard module file structure: `main.tf`, `variables.tf`, `outputs.tf`, `versions.tf`, `locals.tf`233. Write `versions.tf` with `required_version` and `required_providers` blocks pinned to stable versions244. Define input variables in `variables.tf` with descriptions, types, defaults, and validation rules255. Implement resources in `main.tf` using variables for all configurable values; use `locals` for computed values266. Add meaningful outputs in `outputs.tf` for resource IDs, ARNs, endpoints, and connection strings277. Implement security defaults: encryption enabled, public access blocked, least-privilege IAM, logging enabled288. Create an `examples/` directory with at least one complete usage example showing module invocation299. Generate documentation with `terraform-docs markdown table . > README.md`3010. Validate the module with `terraform init && terraform validate` and test with `terraform plan` using the example3132## Output3334- Module files: `main.tf`, `variables.tf`, `outputs.tf`, `versions.tf`, `locals.tf`35- Example usage in `examples/basic/main.tf` with realistic variable values36- Auto-generated `README.md` with inputs, outputs, and usage documentation37- `.terraform-docs.yml` configuration for documentation generation38- Optional: test files using Terratest or `terraform test` (HCL-based)3940## Error Handling4142| Error | Cause | Solution |43|-------|-------|---------|44| `variable validation failed` | Input value does not meet validation rule | Check the `validation` block in `variables.tf`; adjust the value or the validation regex |45| `provider not found` | Missing or wrong provider source in `versions.tf` | Add the provider to `required_providers` with correct source and version constraint |46| `circular dependency` | Resources referencing each other in a loop | Refactor to break the cycle; use `depends_on` or separate into sub-modules |47| `output references undeclared resource` | Typo in resource name or resource removed | Verify resource names in `main.tf` match output references exactly |48| `module source not found` | Incorrect module path or registry reference | Verify the `source` path is relative (e.g., `./modules/vpc`) or a valid registry address |4950## Examples5152- "Build a Terraform module for an AWS VPC with configurable CIDR, public/private subnets across 3 AZs, NAT gateway, and flow logs."53- "Create a GCP Cloud Run module with custom domain, IAM bindings, and auto-scaling configuration as input variables."54- "Generate a Terraform module for an S3 bucket with versioning, encryption, lifecycle rules, and access logging, with all settings as optional variables with secure defaults."5556## Resources5758- Terraform module registry: https://registry.terraform.io/59- Module structure guide: https://developer.hashicorp.com/terraform/language/modules/develop/structure60- terraform-docs: https://terraform-docs.io/61- Module best practices: https://developer.hashicorp.com/terraform/language/modules/develop6263---64> Converted and distributed by [TomeVault](https://tomevault.io/claim/jeremylongshore) — claim your Tome and manage your conversions.65<!-- tomevault:4.0:skill_md:2026-04-11 -->