Skip to main content
skill CX Codex

terraform-best-practices · nickcrew — Claude Code skill: what it does

terraform-best-practices adds enterprise Terraform guidelines covering module layout, secure remote state, workspace isolation, and CI/CD security scanning.

MIT 109 installs ★ 71 updated 2026-06-29

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add NickCrew/Claude-Cortex --skill terraform-best-practices -a claude-code
Open on GitHub

What terraform-best-practices does

This skill instructs coding agents on production-level IaC patterns for Terraform and Terragrunt. It triggers on **/*.tf files and **/terraform/** directory paths. It replaces monolithic scripts with reusable modules organized around standard files: main.tf, variables.tf, outputs.tf, versions.tf, and README.md.

Guidance includes remote state storage using S3 with KMS encryption and DynamoDB locking. For environment isolation, it favors directory structures for production over plain workspaces. It also lays out deployment stages using terraform plan -out=tfplan, pre-commit hooks, policy checks via OPA or Sentinel, and code audits with tfsec and Checkov.

Execution steps referencing external tooling such as checkov or Terratest will fail if those binaries and Go are not installed locally.

What it helps with

  • Structure reusable Terraform modules using standard file conventions
  • Configure remote state backends with S3, KMS encryption, and DynamoDB locks
  • Isolate production environments from staging using directories or Terragrunt
  • Add automated security scanning with tfsec and Checkov to CI/CD workflows

Example prompts for the agent

  • Refactor this network code into child modules with input validations
  • Set up backend.tf with remote S3 state and DynamoDB locking
  • Create a deployment checklist using plan review and tfsec scanning

How to install terraform-best-practices

1Skills CLI (Vercel)

npx skills add NickCrew/Claude-Cortex --skill terraform-best-practices -a claude-code

2Manually: put the files in place

~/.claude/skills/terraform-best-practices/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/terraform-best-practices/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about terraform-best-practices

How to install terraform-best-practices in Claude Code?

Run npx skills add NickCrew/Claude-Cortex --skill terraform-best-practices -a claude-code in your terminal.

What does terraform-best-practices do?

It provides architectural rules and conventions for writing maintainable HCL modules, locking backend state, and testing infrastructure code.

How does this skill handle environment separation?

It prescribes directory-based isolation for critical production infrastructure and reserves Terraform workspaces for similar temporary setups.

Source text

SKILL.md · MIT
---
name: terraform-best-practices
description: 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.
keywords:
  - HCL
  - IaC
  - infrastructure as code
  - infrastructure automation
  - remote state
  - state management
  - terraform
  - terraform module
  - terraform workspace
  - terragrunt
file_patterns:
  - '**/*.tf'
  - '**/terraform/**'
confidence: 0.9
---

# 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
```bash
# 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
```bash
# 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
```bash
# Mark sensitive variables
# Use secret management for credentials
# Configure state encryption
# Set up security scanning in CI/CD
```

### 4. Testing Pipeline
```bash
# 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
```bash
# 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
```bash
# 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

- **Official Docs**: https://developer.hashicorp.com/terraform/docs
- **Style Guide**: https://developer.hashicorp.com/terraform/language/syntax/style
- **Module Registry**: https://registry.terraform.io/
- **Terragrunt**: https://terragrunt.gruntwork.io/
- **Terratest**: https://terratest.gruntwork.io/
- **tfsec**: https://aquasecurity.github.io/tfsec/
- **Checkov**: https://www.checkov.io/
- **Best Practices**: https://www.terraform-best-practices.com/
- **AWS Provider**: https://registry.terraform.io/providers/hashicorp/aws/latest/docs

© NickCrew · MIT · NickCrew/Claude-Cortex

Similar skills

All skills for DevOps and infrastructure →

terraform-best-practices by other authors comes first.

terraform-best-practices c0x12c The terraform-best-practices skill guides Terraform code reviews and authoring, covering file layout, modules, state, and security. Use it when shaping infrastructure configuration. CX Codex +1 ★ 111repo stars terraform-expert PCL Team terraform-expert — a skill for Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, and OpenCode that gives the agent deep Terraform knowledge: modules, remote state, and resource lifecycle. CX Codex +1 190installs terraform-generator akin-ozer The terraform-generator skill writes Terraform configs: resources, modules, providers, variables and outputs. Useful when you want infrastructure as code without hand-writing HCL. CX Codex +1 713installs terraform microsoft Sets conventions for Terraform files, resource names, modules, and expressions. It applies to .tf, .tfvars, and files under terraform/. CX Codex +1 ★ 1,627repo stars terraform-skill Anton Babenko Guides Terraform and OpenTofu refactoring, state migrations, and CI workflows. Diagnoses risks like identity churn and blast radius before generating HCL. CX Codex +1 8,200installs terraform-stacks hashicorp Guides AI coding assistants through HashiCorp Terraform Stacks syntax, component definitions, and multi-environment deployment configs. CX Codex +1 5,900installs terraform-infrastructure manutej terraform-infrastructure guides the AI agent in writing HashiCorp Configuration Language code for AWS, Azure, and GCP, covering reusable modules, variable validation, and multi-environment state. CX Codex +1 439installs

From the same set21

The whole set →
secure-coding-practices secure-coding-practices is a Claude Code skill for applying defensive coding patterns to authentication, user input, and sensitive data. It also supports security-focused code reviews. CX Codex +1 134installs ux-review ux-review reviews a component or flow from three angles: usability heuristics, WCAG 2.1 AA accessibility and interaction design. Comes back with a prioritized report. CX Codex +1 119installs event-driven-architecture The event-driven-architecture skill guides event-driven system design with event sourcing, CQRS, sagas, and message brokers. Use it for microservices and distributed transactions. CX Codex +1 118installs dashboard-designer The dashboard-designer Claude Code skill plans dashboard KPIs, layout, and visual hierarchy. It activates for requests about monitoring dashboards or choosing a BI tool. CX Codex +1 109installs mermaid-diagramming mermaid-diagramming gets the agent to write Mermaid diagrams for docs: flowcharts, sequence and ER diagrams, state machines, Gantt charts and architecture views. CX Codex +1 105installs kubernetes-security-policies kubernetes-security-policies — a coding-agent skill that walks through Kubernetes hardening: pod security standards, network policies, RBAC. Useful when a cluster needs tightening before production. CX Codex +1 104installs user-journey-mapping The user-journey-mapping Claude Code skill lays out user journeys with stages, touchpoints, emotions, and friction points. It activates for experience mapping or conversion-flow analysis. CX Codex +1 102installs tutorial-design tutorial-design guides you through writing hands-on tutorials, workshops, and coding guides with explicit checkpoints and troubleshooting sections. CX Codex +1 102installs

Project sponsors

Companies that keep this analytics open