Back to rules

Terraform Infrastructure as Code

Verified Advanced ~440 tokens MIT License
598 upvotes 20,400 views by ClaudeHub Team
Works with:
terraform aws gcp azure
CLAUDE.md
You are an expert in Terraform, AWS, and infrastructure automation.Key Principles- Write modular, reusable Terraform configurations.- Use remote state with locking for team collaboration.- Follow the principle of least privilege for IAM.- Version control all infrastructure code.Project Structure- Organize by environment: environments/dev/, environments/prod/.- Use modules/ for reusable components.- Keep providers and backend config in versions.tf.- Use terraform.tfvars for environment-specific values.Module Design- Create focused modules that do one thing well.- Use input variables with descriptions and types.- Define meaningful outputs for module consumers.- Document modules with README and examples.State Management- Use remote state backend (S3, GCS, Terraform Cloud).- Enable state locking to prevent concurrent modifications.- Use workspaces or separate state files per environment.- Never commit terraform.tfstate to version control.Best Practices- Use data sources to reference existing resources.- Use locals for computed values and reduce repetition.- Tag all resources consistently for cost tracking.- Use count and for_each for resource iteration.Security- Never hardcode secrets; use AWS Secrets Manager or Vault.- Use separate AWS accounts per environment.- Enable CloudTrail for audit logging.- Implement VPC with private subnets for databases.CI/CD- Run terraform fmt and terraform validate in CI.- Use terraform plan output as PR comment.- Require approval for terraform apply.- Use tflint and checkov for additional validation.AWS Specifics- Use latest AWS provider version; pin major version.- Prefer managed services over self-hosted.- Enable encryption at rest and in transit.- Use Auto Scaling for variable workloads.

How to use this rule

  1. Copy the content above
  2. Create or open your CLAUDE.md file in your project root
  3. Paste the content and customize as needed
  4. Claude Code will automatically use these instructions