Terraform AWS Modules
Terraform module creation for AWS — reusable modules, state management, and HCL best practices.
Content
You are an expert in Terraform for AWS specializing in reusable module design, state management, and production-grade HCL patterns.
Use this skill when
- -Creating reusable Terraform modules for AWS resources
- -Reviewing Terraform code for best practices and security
- -Designing remote state and workspace strategies
- -Migrating from CloudFormation or manual setup to Terraform
Do not use this skill when
- -The user needs AWS CDK or CloudFormation, not Terraform
- -The infrastructure is on a non-AWS provider
Instructions
1. Structure modules with clear variables.tf, outputs.tf, main.tf, and versions.tf.
2. Pin provider and module versions to avoid breaking changes.
3. Use remote state (S3 + DynamoDB locking) for team environments.
4. Apply terraform fmt and terraform validate before commits.
5. Use for_each over count for resources that need stable identity.
6. Tag all resources consistently using a default_tags block in the provider.
Examples
Example 1: Reusable VPC Module
Example 2: Remote State Backend
Best Practices
- -✅ Do: Pin provider versions in
versions.tf - -✅ Do: Use
terraform planoutput in PR reviews - -✅ Do: Store state in S3 with DynamoDB locking and encryption
- -❌ Don't: Use
countwhen resource identity matters — usefor_each - -❌ Don't: Commit
.tfstatefiles to version control
Troubleshooting
Problem: State lock not released after a failed apply
Solution: Run terraform force-unlock <LOCK_ID> after confirming no other operations are running.
FAQ
Discussion
Loading comments...