Write a Reusable Terraform Module for AWS Infrastructure

Build a reusable, production-grade Terraform module for AWS services with variables, validation, outputs, and usage examples.

๐Ÿ“ The Prompt

Create a reusable Terraform module for provisioning [AWS_SERVICE] (e.g., ECS Fargate cluster, RDS PostgreSQL instance, VPC with subnets, S3 + CloudFront static site) on AWS. The module should follow Terraform best practices and be ready for use in [ENVIRONMENT_TYPES] (e.g., dev, staging, production). Module structure: 1. **`variables.tf`**: - Define all input variables with `description`, `type`, and sensible `default` values where appropriate. - Key variables should include: [KEY_VARIABLES_LIST] (e.g., instance_size, vpc_cidr, enable_encryption, backup_retention_days). - Use `validation` blocks for critical inputs (e.g., CIDR format, allowed instance types). - Include a `tags` variable of type `map(string)` that merges with default tags. 2. **`main.tf`**: - Define the core [AWS_SERVICE] resources with all security best practices enabled (encryption at rest, encryption in transit, logging). - Use `locals` for computed values and tag merging. - Include IAM roles/policies following least-privilege principle. - Add security group rules scoped to [NETWORK_ACCESS_PATTERN] (e.g., VPC-only, specific CIDR ranges). 3. **`outputs.tf`**: - Export key resource attributes: ARN, endpoint, ID, and any connection strings. - Include `sensitive = true` for outputs containing secrets. 4. **`versions.tf`**: - Pin AWS provider version and set minimum Terraform version to [TF_VERSION]. 5. **Usage Example**: - Provide a `examples/basic/main.tf` showing how to call this module with typical values. - Show how to reference outputs in a root module. Ensure all resources use `name_prefix` or naming conventions incorporating `var.environment` and `var.project_name`. Include `lifecycle` blocks where appropriate (e.g., `prevent_destroy` for databases).

๐Ÿ’ก Tips for Better Results

Be specific about which AWS service you need โ€” 'ECS Fargate with ALB' yields far better results than just 'compute.' Always review generated IAM policies for overly broad permissions before applying. Test the module with `terraform plan` in a sandbox account first.

๐ŸŽฏ Use Cases

Cloud engineers and platform teams use this when building standardized, reusable infrastructure modules that enforce security and naming conventions across multiple environments.

๐Ÿ”— Related Prompts

๐Ÿ’ป Coding beginner

Explain Code Like Im a Beginner

Get any code explained in plain English with line-by-line breakdowns, analogies, and learning suggestions.

๐Ÿ’ป Coding beginner

Debug My Code and Explain the Fix

Get your code debugged with clear explanations of what went wrong and why, plus the corrected version.

๐Ÿ’ป Coding intermediate

Write Unit Tests for My Code

Generate thorough unit tests covering edge cases, error handling, and both positive and negative scenarios.

๐Ÿ’ป Coding intermediate

Convert Code Between Languages

Convert code between any programming languages while maintaining idiomatic patterns and best practices.

๐Ÿ’ป Coding intermediate

Write a REST API Endpoint

Generate production-ready REST API endpoints with validation, error handling, and documentation.

๐Ÿ’ป Coding advanced

Create a GitHub Actions CI/CD Workflow for Automated Testing and Deployment

Generate a complete GitHub Actions CI/CD workflow with build, test, deploy, and notification jobs for your project.