Overview
Builds well-structured, reusable Terraform modules following best practices: main.tf, variables.tf (with validation), outputs.tf, versions.tf, remote state backend configuration, and a complete example module for a common pattern (VPC + EC2 + S3 bucket, or equivalent on other clouds).
When to Use This Skill
- Provisioning or refactoring cloud infrastructure as code.
- Creating reusable components that multiple teams or projects will use.
- The user says "Terraform", "IaC", "provision AWS", "create VPC", etc.
Prerequisites
- Terraform >= 1.5 installed.
- Cloud provider credentials configured (AWS profile, GCP application default, Azure CLI).
- Remote state backend (S3 + DynamoDB for AWS, GCS for GCP, etc.).
Steps
Module structure:
modules/vpc/
main.tf
variables.tf
outputs.tf
versions.tf
variables.tf:
- Descriptive descriptions.
type constraints.
default where safe.
validation blocks for important vars.
main.tf:
- Resources with consistent naming (use
local.name or var.name prefix).
- Data sources.
- Locals for computed values.
outputs.tf:
- Only expose what consumers need.
- Sensitive outputs marked.
State & backend:
- Recommend remote backend from day one.
- Provide
backend.tf example (commented for module, required in root).
Complete example:
- A full root module that uses the VPC module + creates an EC2 instance + S3 bucket with proper IAM.
Output:
- All module files.
- Example usage in a root
main.tf.
terraform init/plan/apply commands.
- State migration notes.
Examples
A complete, reusable vpc module for AWS (with public/private subnets, NAT, route tables, security groups) plus a root example that provisions a small web server behind it and an S3 bucket is included.
Edge Cases & Error Handling
- Existing resources: Use
terraform import guidance.
- Multi-region / multi-account: Show workspace or provider alias patterns.
- Drift detection: Regular
terraform plan in CI.
Verification
terraform init.
terraform plan — clean plan.
terraform apply — resources created.
terraform destroy — cleans up.
- Module can be called from another repo with
source = "git::..." or Terraform Registry.
- Success: Infrastructure is reproducible, state is remote and locked, variables are validated, and the module is easy to reuse.
References
1---2name: terraform-module-builder3description: Creates reusable Terraform modules for cloud infrastructure. Use when provisioning AWS, GCP, or Azure resources with Infrastructure as Code.4license: Apache-2.05compatibility: Claude Code, Codex, Gemini CLI6metadata:7 author: ai-skills8 version: "1.0"9 category: devops10 tags: terraform, iac, aws, gcp, azure, modules, state11---1213## Overview1415Builds well-structured, reusable Terraform modules following best practices: `main.tf`, `variables.tf` (with validation), `outputs.tf`, `versions.tf`, remote state backend configuration, and a complete example module for a common pattern (VPC + EC2 + S3 bucket, or equivalent on other clouds).1617## When to Use This Skill1819- Provisioning or refactoring cloud infrastructure as code.20- Creating reusable components that multiple teams or projects will use.21- The user says "Terraform", "IaC", "provision AWS", "create VPC", etc.2223## Prerequisites2425- Terraform >= 1.5 installed.26- Cloud provider credentials configured (AWS profile, GCP application default, Azure CLI).27- Remote state backend (S3 + DynamoDB for AWS, GCS for GCP, etc.).2829## Steps30311. **Module structure**:32 ```33 modules/vpc/34 main.tf35 variables.tf36 outputs.tf37 versions.tf38 ```39402. **variables.tf**:41 - Descriptive descriptions.42 - `type` constraints.43 - `default` where safe.44 - `validation` blocks for important vars.45463. **main.tf**:47 - Resources with consistent naming (use `local.name` or `var.name` prefix).48 - Data sources.49 - Locals for computed values.50514. **outputs.tf**:52 - Only expose what consumers need.53 - Sensitive outputs marked.54555. **State & backend**:56 - Recommend remote backend from day one.57 - Provide `backend.tf` example (commented for module, required in root).58596. **Complete example**:60 - A full root module that uses the VPC module + creates an EC2 instance + S3 bucket with proper IAM.61627. **Output**:63 - All module files.64 - Example usage in a root `main.tf`.65 - `terraform init/plan/apply` commands.66 - State migration notes.6768## Examples6970A complete, reusable `vpc` module for AWS (with public/private subnets, NAT, route tables, security groups) plus a root example that provisions a small web server behind it and an S3 bucket is included.7172## Edge Cases & Error Handling7374- **Existing resources**: Use `terraform import` guidance.75- **Multi-region / multi-account**: Show workspace or provider alias patterns.76- **Drift detection**: Regular `terraform plan` in CI.7778## Verification79801. `terraform init`.812. `terraform plan` — clean plan.823. `terraform apply` — resources created.834. `terraform destroy` — cleans up.845. Module can be called from another repo with `source = "git::..."` or Terraform Registry.856. Success: Infrastructure is reproducible, state is remote and locked, variables are validated, and the module is easy to reuse.8687## References8889- [Terraform Documentation](https://developer.hashicorp.com/terraform/docs)90- [Terraform Module Best Practices](https://developer.hashicorp.com/terraform/language/modules/develop)91- [AWS Provider](https://registry.terraform.io/providers/hashicorp/aws/latest)