Terraform & OpenTofu Skill
Comprehensive Terraform and OpenTofu guidance covering testing, modules, CI/CD, security, and production patterns.
When to Use This Skill
Activate when:
- Creating new Terraform or OpenTofu configurations or modules
- Setting up testing infrastructure for IaC code
- Deciding between testing approaches (validate, plan, frameworks)
- Structuring multi-environment deployments
- Implementing CI/CD for infrastructure-as-code
- Reviewing or refactoring existing Terraform/OpenTofu projects
Don't use for:
- Basic Terraform syntax questions (Claude knows this)
- Provider-specific API reference (link to docs instead)
- Cloud platform questions unrelated to Terraform/OpenTofu
Reference Guide
Load detailed guidance based on context:
| Topic |
Reference |
Load When |
| Code Patterns |
references/code-patterns.md |
Block ordering, count vs for_each, modern features, refactoring |
| Module Patterns |
references/module-patterns.md |
Module hierarchy, variable/output best practices, anti-patterns |
| State Management |
references/state-management.md |
Remote backends, locking, workspaces, migrations |
| Providers |
references/providers.md |
AWS/Azure/GCP configuration, authentication, Kubernetes/Helm |
| Testing Frameworks |
references/testing-frameworks.md |
Native tests (1.6+), Terratest, set-type blocks, mocking |
| Testing |
references/testing.md |
Plan validation, OPA/Sentinel policy, TFLint, pre-commit |
| CI/CD Workflows |
references/ci-cd-workflows.md |
GitHub Actions, GitLab CI, Infracost, Atlantis, cleanup |
| Security & Compliance |
references/security-compliance.md |
Trivy, Checkov, secrets management, compliance checklists |
| Best Practices |
references/best-practices.md |
DRY patterns, naming, cost optimization, tagging |
| Quick Reference |
references/quick-reference.md |
Cheat sheets, decision flowcharts, troubleshooting, migration |
Core Principles
Module Hierarchy
| Type |
When to Use |
Scope |
| Resource Module |
Single logical group of connected resources |
VPC + subnets, Security group + rules |
| Infrastructure Module |
Collection of resource modules for a purpose |
Multiple resource modules in one region/account |
| Composition |
Complete infrastructure |
Spans multiple regions/accounts |
Hierarchy: Resource → Resource Module → Infrastructure Module → Composition
Naming Conventions
# Descriptive, contextual names
resource "aws_instance" "web_server" { }
resource "aws_s3_bucket" "application_logs" { }
# "this" for singleton resources in modules
resource "aws_vpc" "this" { }
# Variables: context-specific, snake_case
var.vpc_cidr_block # Not just "cidr"
var.database_instance_class # Not just "instance_class"
Code Structure Standards
Resource block ordering:
count or for_each FIRST (blank line after)
- Other arguments
tags as last real argument
depends_on after tags (if needed)
lifecycle at the very end (if needed)
Variable block ordering:
description (ALWAYS required)
type
default
sensitive / nullable
validation
Testing Strategy
Decision Matrix
| Situation |
Approach |
Tools |
Cost |
| Quick syntax check |
Static analysis |
terraform validate, fmt |
Free |
| Pre-commit validation |
Static + lint |
validate, tflint, trivy, checkov |
Free |
| Terraform 1.6+, simple logic |
Native test framework |
Built-in terraform test |
Free-Low |
| Pre-1.6, or Go expertise |
Integration testing |
Terratest |
Low-Med |
| Security/compliance focus |
Policy as code |
OPA, Sentinel |
Free |
| Cost-sensitive workflow |
Mock providers (1.7+) |
Native tests + mocking |
Free |
Testing Pyramid
/\
/ \ End-to-End (Expensive)
/____\ Full environment deployment
/ \
/________\ Integration (Moderate)
/ \ Module testing in isolation
/____________\
/ \ Static Analysis (Cheap)
/________________\ validate, fmt, lint, security scanning
Modern Terraform Features
| Feature |
Version |
Use Case |
try() function |
0.13+ |
Safe fallbacks |
nullable = false |
1.1+ |
Prevent null values |
moved blocks |
1.1+ |
Refactor without destroy |
optional() with defaults |
1.3+ |
Optional object attributes |
| Native testing |
1.6+ |
Built-in test framework |
| Mock providers |
1.7+ |
Cost-free unit testing |
| Provider functions |
1.8+ |
Provider-specific transforms |
| Cross-variable validation |
1.9+ |
Validate between variables |
| Write-only arguments |
1.11+ |
Secrets never stored in state |
Version Verification
CRITICAL: Before proposing any Terraform configuration, you MUST:
- Check the Terraform version:
terraform version -json | python3 -c "import json,sys; print(json.load(sys.stdin)['terraform_version'])"
- Check provider versions:
terraform providers
- Never assume feature availability from training data
- Search official documentation if in doubt
CI/CD Integration
Recommended Stages
- Validate — format + syntax + linting + security scanning
- Test — automated tests (native or Terratest)
- Plan — generate and review execution plan
- Apply — execute changes (with approvals for production)
Cost Optimization
- Use mocking for PR validation (free)
- Run integration tests only on main branch (controlled cost)
- Implement auto-cleanup (prevent orphaned resources)
- Tag all test resources (track spending)
Security Essentials
# Static security scanning
trivy config .
checkov -d .
Avoid: secrets in variables, default VPC, missing encryption, open security groups.
Use: AWS Secrets Manager, dedicated VPCs, encryption at rest, least-privilege SGs.
Constraints
MUST DO
- Use semantic versioning for modules
- Enable remote state with locking
- Validate inputs with validation blocks
- Use consistent naming conventions
- Tag all resources for cost tracking
- Pin provider versions
- Run terraform fmt and validate
- Include
description on all variables and outputs
MUST NOT DO
- Store secrets in plain text or state
- Use local state for production
- Skip state locking
- Hardcode environment-specific values
- Create circular module dependencies
- Commit .terraform directories
- Use
count when items may be reordered (use for_each)
Attribution
Based on antonbabenko/terraform-skill and jeffallan/claude-skills. Additional resources: terraform-best-practices.com.
Source: jdiegosierra/enterprise-agent-plugins — distributed by TomeVault.
1---2name: terraform-373description: Use when working with Terraform or OpenTofu — creating modules, writing tests, setting up CI/CD pipelines, reviewing configurations, debugging state issues, implementing security scanning, or making IaC architecture decisions. For Pulumi see pulumi-best-practices, for general CI/CD see devops.4---56# Terraform & OpenTofu Skill78Comprehensive Terraform and OpenTofu guidance covering testing, modules, CI/CD, security, and production patterns.910## When to Use This Skill1112**Activate when:**13- Creating new Terraform or OpenTofu configurations or modules14- Setting up testing infrastructure for IaC code15- Deciding between testing approaches (validate, plan, frameworks)16- Structuring multi-environment deployments17- Implementing CI/CD for infrastructure-as-code18- Reviewing or refactoring existing Terraform/OpenTofu projects1920**Don't use for:**21- Basic Terraform syntax questions (Claude knows this)22- Provider-specific API reference (link to docs instead)23- Cloud platform questions unrelated to Terraform/OpenTofu2425## Reference Guide2627Load detailed guidance based on context:2829| Topic | Reference | Load When |30|-------|-----------|-----------|31| Code Patterns | `references/code-patterns.md` | Block ordering, count vs for_each, modern features, refactoring |32| Module Patterns | `references/module-patterns.md` | Module hierarchy, variable/output best practices, anti-patterns |33| State Management | `references/state-management.md` | Remote backends, locking, workspaces, migrations |34| Providers | `references/providers.md` | AWS/Azure/GCP configuration, authentication, Kubernetes/Helm |35| Testing Frameworks | `references/testing-frameworks.md` | Native tests (1.6+), Terratest, set-type blocks, mocking |36| Testing | `references/testing.md` | Plan validation, OPA/Sentinel policy, TFLint, pre-commit |37| CI/CD Workflows | `references/ci-cd-workflows.md` | GitHub Actions, GitLab CI, Infracost, Atlantis, cleanup |38| Security & Compliance | `references/security-compliance.md` | Trivy, Checkov, secrets management, compliance checklists |39| Best Practices | `references/best-practices.md` | DRY patterns, naming, cost optimization, tagging |40| Quick Reference | `references/quick-reference.md` | Cheat sheets, decision flowcharts, troubleshooting, migration |4142## Core Principles4344### Module Hierarchy4546| Type | When to Use | Scope |47|------|-------------|-------|48| **Resource Module** | Single logical group of connected resources | VPC + subnets, Security group + rules |49| **Infrastructure Module** | Collection of resource modules for a purpose | Multiple resource modules in one region/account |50| **Composition** | Complete infrastructure | Spans multiple regions/accounts |5152**Hierarchy:** Resource → Resource Module → Infrastructure Module → Composition5354### Naming Conventions5556```hcl57# Descriptive, contextual names58resource "aws_instance" "web_server" { }59resource "aws_s3_bucket" "application_logs" { }6061# "this" for singleton resources in modules62resource "aws_vpc" "this" { }6364# Variables: context-specific, snake_case65var.vpc_cidr_block # Not just "cidr"66var.database_instance_class # Not just "instance_class"67```6869### Code Structure Standards7071**Resource block ordering:**721. `count` or `for_each` FIRST (blank line after)732. Other arguments743. `tags` as last real argument754. `depends_on` after tags (if needed)765. `lifecycle` at the very end (if needed)7778**Variable block ordering:**791. `description` (ALWAYS required)802. `type`813. `default`824. `sensitive` / `nullable`835. `validation`8485## Testing Strategy8687### Decision Matrix8889| Situation | Approach | Tools | Cost |90|-----------|----------|-------|------|91| Quick syntax check | Static analysis | `terraform validate`, `fmt` | Free |92| Pre-commit validation | Static + lint | `validate`, `tflint`, `trivy`, `checkov` | Free |93| Terraform 1.6+, simple logic | Native test framework | Built-in `terraform test` | Free-Low |94| Pre-1.6, or Go expertise | Integration testing | Terratest | Low-Med |95| Security/compliance focus | Policy as code | OPA, Sentinel | Free |96| Cost-sensitive workflow | Mock providers (1.7+) | Native tests + mocking | Free |9798### Testing Pyramid99100```101 /\102 / \ End-to-End (Expensive)103 /____\ Full environment deployment104 / \105 /________\ Integration (Moderate)106 / \ Module testing in isolation107 /____________\108 / \ Static Analysis (Cheap)109/________________\ validate, fmt, lint, security scanning110```111112## Modern Terraform Features113114| Feature | Version | Use Case |115|---------|---------|----------|116| `try()` function | 0.13+ | Safe fallbacks |117| `nullable = false` | 1.1+ | Prevent null values |118| `moved` blocks | 1.1+ | Refactor without destroy |119| `optional()` with defaults | 1.3+ | Optional object attributes |120| Native testing | 1.6+ | Built-in test framework |121| Mock providers | 1.7+ | Cost-free unit testing |122| Provider functions | 1.8+ | Provider-specific transforms |123| Cross-variable validation | 1.9+ | Validate between variables |124| Write-only arguments | 1.11+ | Secrets never stored in state |125126## Version Verification127128**CRITICAL**: Before proposing any Terraform configuration, you MUST:1291301. **Check the Terraform version**:131 ```bash132 terraform version -json | python3 -c "import json,sys; print(json.load(sys.stdin)['terraform_version'])"133 ```1342. **Check provider versions**: `terraform providers`1353. **Never assume feature availability from training data**1364. **Search official documentation if in doubt**137138## CI/CD Integration139140### Recommended Stages1411421. **Validate** — format + syntax + linting + security scanning1432. **Test** — automated tests (native or Terratest)1443. **Plan** — generate and review execution plan1454. **Apply** — execute changes (with approvals for production)146147### Cost Optimization1481491. Use mocking for PR validation (free)1502. Run integration tests only on main branch (controlled cost)1513. Implement auto-cleanup (prevent orphaned resources)1524. Tag all test resources (track spending)153154## Security Essentials155156```bash157# Static security scanning158trivy config .159checkov -d .160```161162**Avoid:** secrets in variables, default VPC, missing encryption, open security groups.163**Use:** AWS Secrets Manager, dedicated VPCs, encryption at rest, least-privilege SGs.164165## Constraints166167### MUST DO168- Use semantic versioning for modules169- Enable remote state with locking170- Validate inputs with validation blocks171- Use consistent naming conventions172- Tag all resources for cost tracking173- Pin provider versions174- Run terraform fmt and validate175- Include `description` on all variables and outputs176177### MUST NOT DO178- Store secrets in plain text or state179- Use local state for production180- Skip state locking181- Hardcode environment-specific values182- Create circular module dependencies183- Commit .terraform directories184- Use `count` when items may be reordered (use `for_each`)185186## Attribution187188Based on [antonbabenko/terraform-skill](https://github.com/antonbabenko/terraform-skill) and [jeffallan/claude-skills](https://github.com/jeffallan/claude-skills). Additional resources: [terraform-best-practices.com](https://terraform-best-practices.com).189190---191> Source: [jdiegosierra/enterprise-agent-plugins](https://github.com/jdiegosierra/enterprise-agent-plugins) — distributed by [TomeVault](https://tomevault.io).192<!-- tomevault:4.0:skill_md:2026-06-16 -->