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.

๐Ÿ”— 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.