Generate reusable Terraform modules for infrastructure components following HashiCorp best practices
Generate well-structured, reusable Terraform modules for infrastructure components following HashiCorp conventions and best practices.
This skill helps you create standardized Terraform modules that can be shared across teams and projects. Modules encapsulate collections of resources that are managed together, enabling consistent and repeatable infrastructure provisioning.
When a user requests a Terraform module, follow these steps:
1. **Understand Requirements**
- Ask the user what infrastructure resources the module should manage (e.g., VPC, S3 bucket, EKS cluster)
- Identify the cloud provider (AWS, Azure, GCP, etc.)
- Determine input variables needed for customization
- Clarify outputs that should be exposed
2. **Create Module Structure**
- Create a directory for the module (e.g., `modules/vpc/` or `terraform-aws-vpc/`)
- Generate these standard files:
- `main.tf` - Primary resource definitions
- `variables.tf` - Input variable declarations
- `outputs.tf` - Output value declarations
- `versions.tf` - Terraform and provider version constraints
- `README.md` - Module documentation
3. **Write Resource Configuration**
- Define resources in `main.tf` using best practices:
- Use descriptive resource names
- Parameterize values using input variables
- Add resource dependencies explicitly when needed
- Use data sources for existing resources
- Follow HashiCorp naming conventions (snake_case for resources and variables)
4. **Define Variables**
- In `variables.tf`, declare all input variables with:
- Descriptive names and descriptions
- Appropriate types (string, number, bool, list, map, object)
- Default values where sensible
- Validation rules where applicable
- Mark sensitive variables appropriately
5. **Expose Outputs**
- In `outputs.tf`, define outputs for:
- Resource IDs that other modules may need
- Connection information (endpoints, URLs)
- Generated values (passwords, keys - mark as sensitive)
- Include descriptions for all outputs
6. **Set Version Constraints**
- In `versions.tf`, specify:
- Minimum Terraform version required
- Provider versions with appropriate constraints
- Use `~>` for provider versions to allow patch updates
7. **Document the Module**
- In `README.md`, include:
- Overview of what the module creates
- Usage example with module block
- Input variables table (name, description, type, default, required)
- Outputs table (name, description)
- Requirements section (Terraform version, providers)
- Any additional notes or considerations
8. **Add Examples** (optional but recommended)
- Create `examples/` directory with working examples
- Show common use cases
- Include example `terraform.tfvars` files
9. **Validate Structure**
- Run `terraform fmt` to format code consistently
- Run `terraform validate` to check syntax
- Ensure all variables are used and all outputs are valid
```
terraform-aws-vpc/
├── main.tf
├── variables.tf
├── outputs.tf
├── versions.tf
├── README.md
└── examples/
└── complete/
├── main.tf
└── terraform.tfvars.example
```
**User Request:** "Create a Terraform module for an AWS S3 bucket with encryption"
**Your Response:**
1. Create the module directory structure
2. Generate `main.tf` with S3 bucket resource including encryption configuration
3. Define variables in `variables.tf` (bucket name, encryption settings, tags, etc.)
4. Add outputs in `outputs.tf` (bucket ARN, bucket name, bucket URL)
5. Specify AWS provider version in `versions.tf`
6. Write comprehensive README with usage example
7. Format and validate the module
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/terraform-module-generator/raw