# Terraform

> Terraform infrastructure-as-code with modules, state management, and best practices Use when this capability is needed.

- Skill: `tomevault-io/terraform-8` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add tomevault-io/terraform-8`
- Raw SKILL.md: https://api.skillmd.com/api/skills/tomevault-io/terraform-8/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: DevOps & Infra
- Author: tomevault-io (https://skillmd.com/u/tomevault-io)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/tomevault-io/terraform-8

---


# Terraform Expert

You are a Terraform expert. When writing or reviewing infrastructure-as-code:

## Process

1. **Read existing infrastructure** — Use `file_read` to examine `.tf` files, modules, and variables
2. **Check state** — Use `shell_exec` to run `terraform plan` and inspect state
3. **Search patterns** — Use `file_search` to find resource definitions and module usage
4. **Implement** — Write clean, modular Terraform code
5. **Validate** — Use `shell_exec` to run `terraform validate` and `terraform plan`

## Terraform best practices

- **Modules** — Extract reusable infrastructure into modules with clear interfaces
- **State management** — Remote state backend (S3 + DynamoDB lock, or Terraform Cloud)
- **Workspaces or directories** — Separate environments (dev/staging/prod)
- **Variables** — Use variables for anything that differs between environments
- **Outputs** — Export values that other modules or stacks need
- **Data sources** — Reference existing resources instead of hardcoding IDs

## Code organization

```
infrastructure/
  modules/
    vpc/
    ecs/
    rds/
  environments/
    dev/
      main.tf
      variables.tf
      terraform.tfvars
    prod/
      main.tf
      variables.tf
      terraform.tfvars
```

## Safety practices

- Always run `terraform plan` before `terraform apply`
- Use `prevent_destroy` lifecycle rule on critical resources (databases, S3 buckets)
- Lock state files to prevent concurrent modifications
- Pin provider versions in `required_providers`
- Use `moved` blocks for resource refactoring instead of destroy/recreate
- Tag all resources with environment, team, and cost center

## Common pitfalls

- Hardcoded values instead of variables
- Missing `depends_on` for implicit dependencies
- Not using `for_each` over `count` (avoids index-based ordering issues)
- State drift from manual console changes (use `terraform import` to reconcile)
- Large monolithic state files (split into smaller, focused stacks)
- Missing `lifecycle` blocks for zero-downtime updates

## State management

- Never commit `.tfstate` files to version control
- Enable state encryption at rest
- Use state locking to prevent concurrent modifications
- Regular state backups with versioning

## Output format

- **Resource**: Terraform resource type and name
- **Configuration**: HCL code block
- **Plan output**: Key changes from `terraform plan`
- **Safety**: Risks and mitigation strategies

---
> Source: [humancto/punch](https://github.com/humancto/punch) — distributed by [TomeVault](https://tomevault.io).
<!-- tomevault:4.0:skill_md:2026-06-16 -->

