Terraform Best Practices
Expert guidance for building production-grade Terraform infrastructure with enterprise patterns for module design, state management, security, testing, and multi-environment deployments.
When to Use This Skill
- Writing reusable Terraform modules for teams or organizations
- Setting up secure remote state management and backend configuration
- Designing multi-environment infrastructure (dev/staging/prod)
- Implementing infrastructure CI/CD pipelines with automated validation
- Managing infrastructure at scale across multiple teams or projects
- Migrating from manual infrastructure to infrastructure-as-code
- Refactoring existing Terraform for better maintainability
- Implementing security best practices for infrastructure code
Core Concepts
Module Design Philosophy
- Composition over monoliths: Break infrastructure into reusable child modules
- Standard structure: main.tf, variables.tf, outputs.tf, versions.tf, README.md
- Type constraints: Use validation blocks and complex types for safety
- Dynamic blocks: Enable flexible configuration without duplication
State Management Principles
- Remote backends: S3+DynamoDB or Terraform Cloud for team collaboration
- State encryption: KMS encryption at rest and in transit (mandatory)
- State locking: Prevent concurrent modifications with DynamoDB
- Workspace strategy: Directory-based for production, workspaces for similar envs
Security Fundamentals
- Secret management: AWS Secrets Manager, HashiCorp Vault (never hardcode)
- Least privilege: Separate IAM roles per environment
- Security scanning: tfsec, Checkov, Terrascan in CI/CD
- Resource tagging: Enable cost tracking, ownership, compliance
Testing & Validation
- Pre-commit hooks: Format, validate, lint before commits
- Plan review: Always save and review plans before apply
- Automated testing: Terratest for critical infrastructure modules
- Policy as code: OPA/Sentinel for compliance enforcement
Quick Reference
| Task |
Load reference |
| Module structure, variables, outputs, dynamic blocks |
skills/terraform-best-practices/references/module-design.md |
| Remote backends, state encryption, workspace strategies |
skills/terraform-best-practices/references/state-management.md |
| Variable precedence, tfvars, Terragrunt DRY config |
skills/terraform-best-practices/references/environment-management.md |
| Secrets, IAM, scanning tools, resource tagging |
skills/terraform-best-practices/references/security.md |
| Pre-commit hooks, Terratest, policy as code |
skills/terraform-best-practices/references/testing-validation.md |
| Comprehensive checklist for all areas |
skills/terraform-best-practices/references/best-practices-summary.md |
Workflow
1. Project Setup
# Initialize directory structure
mkdir -p {modules,environments/{dev,staging,prod}}
# Set up remote backend (bootstrap S3 + DynamoDB first)
# Configure backend.tf with encryption and locking
2. Module Development
# Create module with standard structure
cd modules/my-module
touch main.tf variables.tf outputs.tf versions.tf README.md
# Add validation to variables
# Use complex types for structured inputs
# Document outputs with descriptions
3. Security Hardening
# Mark sensitive variables
# Use secret management for credentials
# Configure state encryption
# Set up security scanning in CI/CD
4. Testing Pipeline
# Install pre-commit hooks
pre-commit install
# Run validation locally
terraform init
terraform validate
terraform fmt -check
# Security scanning
tfsec .
checkov -d .
# Automated tests (critical modules)
cd tests && go test -v
5. Deployment Process
# Plan with output file
terraform plan -out=tfplan
# Review plan thoroughly
terraform show tfplan
# Apply only after approval
terraform apply tfplan
# Verify deployment
terraform output
6. Multi-Environment Management
# Use directory-based isolation for production
cd environments/prod
terraform init
terraform workspace list
# Or use Terragrunt for DRY backend config
terragrunt plan
Common Mistakes
❌ Hardcoding secrets in code → Use secret management services
❌ No state locking → Enable DynamoDB locking to prevent conflicts
❌ Skipping plan review → Always save and review execution plans
❌ No version constraints → Pin provider and module versions
❌ Local state in teams → Use remote backends for collaboration
❌ No security scanning → Integrate tfsec/Checkov in CI/CD
❌ Missing resource tags → Tag all resources for cost/ownership tracking
❌ No automated testing → Write Terratest for critical modules
❌ Monolithic modules → Break into composable child modules
❌ No backup strategy → Enable S3 versioning on state buckets
Resources
Source: NickCrew/Claude-Cortex — distributed by TomeVault.
1---2name: terraform-best-practices3description: Terraform infrastructure-as-code best practices for scalable and maintainable cloud infrastructure. Use when writing Terraform modules, managing infrastructure state, or implementing infrastructure automation at scale. Use when this capability is needed.4---56# Terraform Best Practices78Expert guidance for building production-grade Terraform infrastructure with enterprise patterns for module design, state management, security, testing, and multi-environment deployments.910## When to Use This Skill1112- Writing reusable Terraform modules for teams or organizations13- Setting up secure remote state management and backend configuration14- Designing multi-environment infrastructure (dev/staging/prod)15- Implementing infrastructure CI/CD pipelines with automated validation16- Managing infrastructure at scale across multiple teams or projects17- Migrating from manual infrastructure to infrastructure-as-code18- Refactoring existing Terraform for better maintainability19- Implementing security best practices for infrastructure code2021## Core Concepts2223### Module Design Philosophy24- **Composition over monoliths**: Break infrastructure into reusable child modules25- **Standard structure**: main.tf, variables.tf, outputs.tf, versions.tf, README.md26- **Type constraints**: Use validation blocks and complex types for safety27- **Dynamic blocks**: Enable flexible configuration without duplication2829### State Management Principles30- **Remote backends**: S3+DynamoDB or Terraform Cloud for team collaboration31- **State encryption**: KMS encryption at rest and in transit (mandatory)32- **State locking**: Prevent concurrent modifications with DynamoDB33- **Workspace strategy**: Directory-based for production, workspaces for similar envs3435### Security Fundamentals36- **Secret management**: AWS Secrets Manager, HashiCorp Vault (never hardcode)37- **Least privilege**: Separate IAM roles per environment38- **Security scanning**: tfsec, Checkov, Terrascan in CI/CD39- **Resource tagging**: Enable cost tracking, ownership, compliance4041### Testing & Validation42- **Pre-commit hooks**: Format, validate, lint before commits43- **Plan review**: Always save and review plans before apply44- **Automated testing**: Terratest for critical infrastructure modules45- **Policy as code**: OPA/Sentinel for compliance enforcement4647## Quick Reference4849| Task | Load reference |50| --- | --- |51| Module structure, variables, outputs, dynamic blocks | `skills/terraform-best-practices/references/module-design.md` |52| Remote backends, state encryption, workspace strategies | `skills/terraform-best-practices/references/state-management.md` |53| Variable precedence, tfvars, Terragrunt DRY config | `skills/terraform-best-practices/references/environment-management.md` |54| Secrets, IAM, scanning tools, resource tagging | `skills/terraform-best-practices/references/security.md` |55| Pre-commit hooks, Terratest, policy as code | `skills/terraform-best-practices/references/testing-validation.md` |56| Comprehensive checklist for all areas | `skills/terraform-best-practices/references/best-practices-summary.md` |5758## Workflow5960### 1. Project Setup61```bash62# Initialize directory structure63mkdir -p {modules,environments/{dev,staging,prod}}6465# Set up remote backend (bootstrap S3 + DynamoDB first)66# Configure backend.tf with encryption and locking67```6869### 2. Module Development70```bash71# Create module with standard structure72cd modules/my-module73touch main.tf variables.tf outputs.tf versions.tf README.md7475# Add validation to variables76# Use complex types for structured inputs77# Document outputs with descriptions78```7980### 3. Security Hardening81```bash82# Mark sensitive variables83# Use secret management for credentials84# Configure state encryption85# Set up security scanning in CI/CD86```8788### 4. Testing Pipeline89```bash90# Install pre-commit hooks91pre-commit install9293# Run validation locally94terraform init95terraform validate96terraform fmt -check9798# Security scanning99tfsec .100checkov -d .101102# Automated tests (critical modules)103cd tests && go test -v104```105106### 5. Deployment Process107```bash108# Plan with output file109terraform plan -out=tfplan110111# Review plan thoroughly112terraform show tfplan113114# Apply only after approval115terraform apply tfplan116117# Verify deployment118terraform output119```120121### 6. Multi-Environment Management122```bash123# Use directory-based isolation for production124cd environments/prod125terraform init126terraform workspace list127128# Or use Terragrunt for DRY backend config129terragrunt plan130```131132## Common Mistakes133134❌ **Hardcoding secrets in code** → Use secret management services135❌ **No state locking** → Enable DynamoDB locking to prevent conflicts136❌ **Skipping plan review** → Always save and review execution plans137❌ **No version constraints** → Pin provider and module versions138❌ **Local state in teams** → Use remote backends for collaboration139❌ **No security scanning** → Integrate tfsec/Checkov in CI/CD140❌ **Missing resource tags** → Tag all resources for cost/ownership tracking141❌ **No automated testing** → Write Terratest for critical modules142❌ **Monolithic modules** → Break into composable child modules143❌ **No backup strategy** → Enable S3 versioning on state buckets144145## Resources146147- **Official Docs**: https://developer.hashicorp.com/terraform/docs148- **Style Guide**: https://developer.hashicorp.com/terraform/language/syntax/style149- **Module Registry**: https://registry.terraform.io/150- **Terragrunt**: https://terragrunt.gruntwork.io/151- **Terratest**: https://terratest.gruntwork.io/152- **tfsec**: https://aquasecurity.github.io/tfsec/153- **Checkov**: https://www.checkov.io/154- **Best Practices**: https://www.terraform-best-practices.com/155- **AWS Provider**: https://registry.terraform.io/providers/hashicorp/aws/latest/docs156157---158> Source: [NickCrew/Claude-Cortex](https://github.com/NickCrew/Claude-Cortex) — distributed by [TomeVault](https://tomevault.io).159<!-- tomevault:4.0:skill_md:2026-06-15 -->