Build a Reusable Terraform Module for AWS Infrastructure
Generate a reusable Terraform module for AWS with variables, validation, outputs, security best practices, and usage documentation.
๐ The Prompt
Create a reusable, production-grade Terraform module for provisioning [AWS_SERVICE] (e.g., ECS Fargate service, RDS Aurora cluster, VPC with subnets, Lambda with API Gateway) on AWS.
**Module Context:**
- Terraform version: >= [TF_VERSION]
- AWS provider version: ~> [PROVIDER_VERSION]
- Purpose: [MODULE_PURPOSE]
- Compliance requirements: [COMPLIANCE_STANDARD] (e.g., SOC2, HIPAA, CIS Benchmarks)
**Generate the following files:**
1. **main.tf**: Core resource definitions with:
- All resources tagged using a local `tags` merge of default and user-provided tags
- Encryption at rest enabled by default using [KMS_STRATEGY] (AWS-managed or CMK)
- Logging and monitoring resources (CloudWatch log groups, metric alarms for [KEY_METRICS])
- Security groups with least-privilege ingress/egress rules
- IAM roles and policies following least-privilege principle with `aws_iam_policy_document` data sources
2. **variables.tf**: Input variables with:
- Descriptive `description` for every variable
- Sensible `default` values where appropriate
- `type` constraints using complex types (object, map, list) where beneficial
- `validation` blocks for [CRITICAL_VARIABLES] (e.g., CIDR format, naming conventions, allowed instance types)
- `sensitive = true` for credentials and secrets
3. **outputs.tf**: Export [KEY_OUTPUTS] (e.g., ARNs, endpoints, security group IDs) with descriptions, marking sensitive outputs appropriately.
4. **versions.tf**: Required providers block with version constraints.
5. **locals.tf**: Computed values, naming conventions using `"${var.project}-${var.environment}-${var.name}"` pattern, and common tag maps.
6. **README.md**: Auto-doc compatible description with usage example showing:
- Minimal configuration
- Full configuration with all optional features enabled
- How to reference outputs from other modules
7. **examples/complete/main.tf**: Working example that calls the module with realistic values.
Ensure the module supports multiple environments (dev/staging/prod) through variables, not hardcoded values. Include inline comments referencing relevant AWS documentation or CIS benchmark control IDs where applicable.
๐ก Tips for Better Results
Pin your AWS provider version explicitly in the prompt โ Terraform resource arguments change between major provider versions and the AI needs this context for accurate code. Include your compliance requirements (SOC2, HIPAA) because this dramatically changes encryption, logging, and access control defaults in the generated resources. Run `terraform validate` and `tflint` on the output immediately, and use `terraform-docs` to auto-generate the final README from the variables and outputs.
๐ฏ Use Cases
Cloud engineers and infrastructure teams use this when building standardized, reusable Terraform modules for their organization's AWS infrastructure, ensuring consistency, security compliance, and self-service provisioning across teams.