ia-terraform
Terraform and OpenTofu configuration, modules, testing, state management, and HCL review. Use when working with Terraform, OpenTofu, HCL, tfvars, tftest, state migration, or IaC patterns.
Install
npx skills add https://github.com/iliaal/whetstone/tree/master/plugins/whetstone/skills/ia-terraform
claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install iliaal-whetstone@llmmart
git clone https://github.com/iliaal/whetstone.git
The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole iliaal/whetstone collection as a plugin from our marketplace. Git is the plain clone.
Skill manifest
Terraform & OpenTofu
Working rules
- Preserve state and resource addresses during refactoring; inspect the plan for unintended replacement.
- Separate plan-only checks from apply-mode tests that create real infrastructure and incur cost.
File Organization & Naming
| File | Purpose |
|---|---|
terraform.tf |
Terraform + provider version requirements |
providers.tf |
Provider configurations |
main.tf |
Primary resources and data sources |
variables.tf |
Input variables (alphabetical) |
outputs.tf |
Output values (alphabetical) |
locals.tf |
Local values |
- Lowercase with underscores:
web_api, notwebAPIorweb-api - Descriptive nouns excluding resource type:
aws_instance.web_apinotaws_instance.web_api_instance - Singular, not plural
thisfor singleton resources (one of that type per module)- Contextual variable prefixes:
vpc_cidr_blocknotcidr
Block Ordering
Resources: count/for_each (blank line after) → arguments → nested blocks → tags → depends_on → lifecycle (last)
Variables: description → type → default → validation → nullable
Every variable needs type + description. Every output needs description. Mark secrets sensitive = true.
Module Structure
| Type | Scope | Example |
|---|---|---|
| Resource Module | Single logical group | VPC + subnets, SG + rules |
| Infrastructure Module | Collection of resource modules | Networking + compute for one region |
| Composition | Complete infrastructure | Spans regions/accounts |
module-name/
├── main.tf, variables.tf, outputs.tf, versions.tf
├── examples/
│ ├── minimal/
│ └── complete/
└── tests/
└── defaults.tftest.hcl
Keep modules small (single responsibility). examples/ double as documentation and integration test fixtures. Semantic versioning for all published modules.
count vs for_each
| Scenario | Use |
|---|---|
| Boolean toggle (create or skip) | count = condition ? 1 : 0 |
| Named/keyed items that may reorder | for_each = toset(list) or map |
| Fixed identical replicas | count = N |
Default to for_each -- removing a middle item from a count list recreates all subsequent resources. Use count only for boolean conditionals or truly identical replicas.
Version Pinning
| Component | Strategy | Example |
|---|---|---|
| Terraform | Pin minor | required_version = "~> 1.9.0" |
| Providers | Pin major | version = "~> 5.0" |
| Modules (prod) | Pin exact | version = "5.1.2" |
| Modules (dev) | Allow patch | version = "~> 5.1.0" |
Key modern features: moved blocks (1.1+), optional() with defaults (1.3+), native testing (1.6+), mock providers (1.7+), cross-variable validation (1.9+), write-only arguments (1.11+).
Stacks (HCP -- check current release status): orchestrates multiple configs as a single deployment unit -- evaluate for multi-environment patterns.
State & Security
- Remote backend with locking: S3 with
use_lockfile = true(1.10+), Azure Blob, GCS, or Terraform Cloud. Never local state for shared infrastructure. DynamoDB-based S3 locking (dynamodb_table) is deprecated and slated for removal -- preferuse_lockfile; both may be set at once while migrating an existing table off. - OpenTofu-only:
terraform { encryption { key_provider "pbkdf2" "k" {...} method "aes_gcm" "m" { keys = key_provider.pbkdf2.k } state { method = method.aes_gcm.m } plan { method = method.aes_gcm.m } } }encrypts state and plan files client-side (or viaTF_ENCRYPTION). Roll out with afallback { method = method.unencrypted.x }so existing plaintext state still loads, and never rename a key provider or method without afallbackblock. OpenTofu also acceptsvar.*/local.*inbackend {}arguments and in modulesource/version(resolved atinit; no state or provider-function references); the same HCL is a hard error in Terraform ("A backend block cannot refer to named values"). - Encrypt state at rest. Never commit
.tfstate,.terraform/, or*.tfplan. Always commit.terraform.lock.hcl. default_tagson provider for consistent resource tagging.- Encryption at rest on all storage. Private networking by default -- public access is opt-in.
- Least-privilege security groups. No
0.0.0.0/0ingress without explicit justification. - Never hardcode credentials -- use assume_role, OIDC, or secrets managers.
- Pre-commit: auto-format first (
terraform fmt -recursive-- rewrites files), then verify (terraform validate && tflint && trivy config .) - Use
movedblocks withfromandtoaddresses for refactoring resource names/modules without destroy-recreate. Retain historical moves for downstream upgrades; remove only after every affected state has migrated, or as an explicitly breaking module release. lifecycle { ignore_changes = [attr] }suppresses updates only, and it substitutes the prior state value at plan time -- on the first plan after the config change, with no "first apply" exception. Two consequences reviewers get backwards: (1) on an already-provisioned resource the literal in the config is never written, andForceNewnever fires becauseignore_changeserased the diff before replacement is evaluated -- so a change that replaces a committed value with a placeholder scrubs the repository and leaves the remote value live; (2)ignore_changesdoes not apply on create, so any later-replace, taint,state rm+ re-add, or manual deletion re-seeds the placeholder over a value that was set out of band. Keep only the container resource in configuration and provision the value entirely out of band, or state the restore step in the runbook for every replace path.
Troubleshooting
- State lock stuck:
terraform force-unlock <ID>-- only after confirming no other operation running - Resource drift:
terraform plan -refresh-onlyto detect,terraform apply -refresh-onlyto accept - Replace tainted:
terraform apply -replace=ADDR(not deprecatedterraform taint) - Import existing:
importblocks (1.5+) for declarative import, orterraform import ADDR ID
Dependency Management
Use locals with try() to control deletion ordering without explicit depends_on:
locals {
vpc_id = try(aws_vpc_ipv4_cidr_block_association.this[0].vpc_id, aws_vpc.this.id, "")
}
This forces Terraform to destroy subnets before CIDR associations -- prevents deletion errors.
cidrsubnet(var.vpc_cidr, 8, count.index)for calculated subnet CIDRs -- never hardcode subnets- Multi-region:
provider "aws" { alias = "eu_west_1" }+providers = { aws = aws.eu_west_1 }in module blocks
Verify
Run before declaring done:
terraform fmt -check && terraform validate && tflint && trivy config .
All commands must pass with zero errors. Where plan-mode tests exist, add terraform test -filter=<unit-test-file> -- restrict this to plan-mode suites, since apply-mode tests stand up real infrastructure and do not belong in a pre-completion check.
Task-specific references
Read the relevant reference before implementing or reviewing the matching behavior:
- For native plan/apply tests, fixture ordering, or CI test selection: native-test-patterns.md.
Files (whetstone)
-
references
-
native-test-patterns.md 2.2 KB
# Native test patterns ## Testing | Situation | Approach | |-----------|----------| | Quick validation | `terraform fmt -check && terraform validate` | | Pre-commit | + `tflint` + `trivy config .` / `checkov -d .` | | Logic validation (1.6+) | Native `terraform test` with `command = plan` | | Cost-free unit tests (1.7+) | Native tests + `mock_provider` | | Real infra validation | Native tests with `command = apply`, or Terratest (Go) | **Native test essentials** (`.tftest.hcl` in `tests/`): - `command = plan` for fast unit tests; `command = apply` for integration (default) - `assert { condition = expr; error_message = "..." }` -- multiple per run block - `expect_failures = [var.name]` for negative testing (validate rejection of bad input) - `mock_provider "aws" { mock_resource "..." { defaults = { ... } } }` -- plan-mode only, no credentials, fast CI - `variables {}` at file level (all runs) or within a `run` block (override) - Reference prior run outputs: `run.setup.vpc_id` - `parallel = true` on independent runs with separate state -- creates sync point at next sequential run - `state_key = "name"` required for `parallel = true` runs with independent state - File naming: `*_unit_test.tftest.hcl` (plan mode) vs `*_integration_test.tftest.hcl` (apply mode) - A `module {}` block inside a `run` accepts local paths and registry modules only -- not git or HTTP sources. Repos consuming git-sourced modules must vendor or localize them before they can be tested. - After a test file completes, resources are destroyed in **reverse run-block order**. Order dependent runs accordingly (create the bucket before the run that puts objects in it), or the destroy fails and leaves billable resources behind. There is no CLI flag to skip cleanup -- inspect a failure with `-verbose`. **Running them:** ```bash terraform test # all *.tftest.hcl under tests/ terraform test -filter=vpc_unit_test.tftest.hcl # one test FILE (not a run-block name) terraform test -verbose # show the plan/apply per run block terraform test -test-directory=path # non-default test dir ``` Split by cost in CI: plan-mode unit tests on every PR, apply-mode integration tests on merge only.
-
-
SKILL.md 7.5 KB
--- name: ia-terraform class: language description: >- Terraform and OpenTofu configuration, modules, testing, state management, and HCL review. Use when working with Terraform, OpenTofu, HCL, tfvars, tftest, state migration, or IaC patterns. paths: "**/*.tf,**/*.tfvars" --- # Terraform & OpenTofu ## Working rules - Preserve state and resource addresses during refactoring; inspect the plan for unintended replacement. - Separate plan-only checks from apply-mode tests that create real infrastructure and incur cost. ## File Organization & Naming | File | Purpose | |------|---------| | `terraform.tf` | Terraform + provider version requirements | | `providers.tf` | Provider configurations | | `main.tf` | Primary resources and data sources | | `variables.tf` | Input variables (alphabetical) | | `outputs.tf` | Output values (alphabetical) | | `locals.tf` | Local values | - Lowercase with underscores: `web_api`, not `webAPI` or `web-api` - Descriptive nouns excluding resource type: `aws_instance.web_api` not `aws_instance.web_api_instance` - Singular, not plural - `this` for singleton resources (one of that type per module) - Contextual variable prefixes: `vpc_cidr_block` not `cidr` ## Block Ordering **Resources:** `count`/`for_each` (blank line after) → arguments → nested blocks → `tags` → `depends_on` → `lifecycle` (last) **Variables:** `description` → `type` → `default` → `validation` → `nullable` Every variable needs `type` + `description`. Every output needs `description`. Mark secrets `sensitive = true`. ## Module Structure | Type | Scope | Example | |------|-------|---------| | Resource Module | Single logical group | VPC + subnets, SG + rules | | Infrastructure Module | Collection of resource modules | Networking + compute for one region | | Composition | Complete infrastructure | Spans regions/accounts | ``` module-name/ ├── main.tf, variables.tf, outputs.tf, versions.tf ├── examples/ │ ├── minimal/ │ └── complete/ └── tests/ └── defaults.tftest.hcl ``` Keep modules small (single responsibility). `examples/` double as documentation and integration test fixtures. Semantic versioning for all published modules. ## count vs for_each | Scenario | Use | |----------|-----| | Boolean toggle (create or skip) | `count = condition ? 1 : 0` | | Named/keyed items that may reorder | `for_each = toset(list)` or `map` | | Fixed identical replicas | `count = N` | Default to `for_each` -- removing a middle item from a `count` list recreates all subsequent resources. Use `count` only for boolean conditionals or truly identical replicas. ## Version Pinning | Component | Strategy | Example | |-----------|----------|---------| | Terraform | Pin minor | `required_version = "~> 1.9.0"` | | Providers | Pin major | `version = "~> 5.0"` | | Modules (prod) | Pin exact | `version = "5.1.2"` | | Modules (dev) | Allow patch | `version = "~> 5.1.0"` | Key modern features: `moved` blocks (1.1+), `optional()` with defaults (1.3+), native testing (1.6+), mock providers (1.7+), cross-variable validation (1.9+), write-only arguments (1.11+). Stacks (HCP -- check current release status): orchestrates multiple configs as a single deployment unit -- evaluate for multi-environment patterns. ## State & Security - Remote backend with locking: S3 with `use_lockfile = true` (1.10+), Azure Blob, GCS, or Terraform Cloud. Never local state for shared infrastructure. DynamoDB-based S3 locking (`dynamodb_table`) is deprecated and slated for removal -- prefer `use_lockfile`; both may be set at once while migrating an existing table off. - OpenTofu-only: `terraform { encryption { key_provider "pbkdf2" "k" {...} method "aes_gcm" "m" { keys = key_provider.pbkdf2.k } state { method = method.aes_gcm.m } plan { method = method.aes_gcm.m } } }` encrypts state and plan files client-side (or via `TF_ENCRYPTION`). Roll out with a `fallback { method = method.unencrypted.x }` so existing plaintext state still loads, and never rename a key provider or method without a `fallback` block. OpenTofu also accepts `var.*`/`local.*` in `backend {}` arguments and in module `source`/`version` (resolved at `init`; no state or provider-function references); the same HCL is a hard error in Terraform ("A backend block cannot refer to named values"). - Encrypt state at rest. Never commit `.tfstate`, `.terraform/`, or `*.tfplan`. Always commit `.terraform.lock.hcl`. - `default_tags` on provider for consistent resource tagging. - Encryption at rest on all storage. Private networking by default -- public access is opt-in. - Least-privilege security groups. No `0.0.0.0/0` ingress without explicit justification. - Never hardcode credentials -- use assume_role, OIDC, or secrets managers. - Pre-commit: auto-format first (`terraform fmt -recursive` -- rewrites files), then verify (`terraform validate && tflint && trivy config .`) - Use `moved` blocks with `from` and `to` addresses for refactoring resource names/modules without destroy-recreate. Retain historical moves for downstream upgrades; remove only after every affected state has migrated, or as an explicitly breaking module release. - `lifecycle { ignore_changes = [attr] }` suppresses **updates only**, and it substitutes the prior state value at plan time -- on the *first* plan after the config change, with no "first apply" exception. Two consequences reviewers get backwards: (1) on an already-provisioned resource the literal in the config is never written, and `ForceNew` never fires because `ignore_changes` erased the diff before replacement is evaluated -- so a change that replaces a committed value with a placeholder scrubs the repository and leaves the remote value live; (2) `ignore_changes` does not apply on create, so any later `-replace`, taint, `state rm` + re-add, or manual deletion re-seeds the placeholder over a value that was set out of band. Keep only the container resource in configuration and provision the value entirely out of band, or state the restore step in the runbook for every replace path. ## Troubleshooting - State lock stuck: `terraform force-unlock <ID>` -- only after confirming no other operation running - Resource drift: `terraform plan -refresh-only` to detect, `terraform apply -refresh-only` to accept - Replace tainted: `terraform apply -replace=ADDR` (not deprecated `terraform taint`) - Import existing: `import` blocks (1.5+) for declarative import, or `terraform import ADDR ID` ## Dependency Management Use `locals` with `try()` to control deletion ordering without explicit `depends_on`: ```hcl locals { vpc_id = try(aws_vpc_ipv4_cidr_block_association.this[0].vpc_id, aws_vpc.this.id, "") } ``` This forces Terraform to destroy subnets before CIDR associations -- prevents deletion errors. - `cidrsubnet(var.vpc_cidr, 8, count.index)` for calculated subnet CIDRs -- never hardcode subnets - Multi-region: `provider "aws" { alias = "eu_west_1" }` + `providers = { aws = aws.eu_west_1 }` in module blocks ## Verify Run before declaring done: ```bash terraform fmt -check && terraform validate && tflint && trivy config . ``` All commands must pass with zero errors. Where plan-mode tests exist, add `terraform test -filter=<unit-test-file>` -- restrict this to plan-mode suites, since apply-mode tests stand up real infrastructure and do not belong in a pre-completion check. ## Task-specific references Read the relevant reference before implementing or reviewing the matching behavior: - For native plan/apply tests, fixture ordering, or CI test selection: [native-test-patterns.md](./references/native-test-patterns.md). -
SPEC.md 4.3 KB
# ia-terraform Specification ## Intent `ia-terraform` is a `language`-class skill (stack-specific patterns and idioms). Terraform and OpenTofu configuration, modules, testing, state management, and HCL review. Use when working with Terraform, OpenTofu, HCL, tfvars, tftest, state migration, or IaC patterns. ## Scope In scope: - Behaviors described in `SKILL.md` and routed via the should_trigger phrasings in `distillery/tests/fixtures/triggers/ia-terraform.jsonl`. - Updates to runtime behavior, structure, trigger precision, references, and validation. Out of scope: - Acting as the runtime instructions themselves (those live in `SKILL.md`). - Trigger phrasings already covered by adjacent `ia-*` skills (`validate-plugin` flags >70% description overlap as DUPLICATE_TRIGGER). - <!-- to fill in: domain-specific exclusions when the skill drifts --> ## Trigger Context - Class: `language` - Hook regex: `plugins/whetstone/hooks/skill-patterns.sh` -> `SKILL_PATTERNS[ia-terraform]` - Common requests (from fixture should_trigger): - "write a terraform module for the VPC and subnets" - "review the infrastructure as code for the staging environment" - "write a Terraform module for the VPC" - Should not trigger for (from fixture should_not_trigger): - "implement the shopping cart feature in React" - "add PHPUnit tests for the order service" - "write a Pulumi program for the same setup" ## Source And Evidence Model Authoritative sources: - `SKILL.md` -- runtime instructions and reference routing. - `references/*.md` -- bundled supplementary content (0 file(s)). - `distillery/tests/fixtures/triggers/ia-terraform.jsonl` -- positive and negative trigger phrasings under regression test. - `plugins/whetstone/hooks/skill-patterns.sh` -- regex pattern that fires this skill. - `distillery/.eval-data/ia-terraform/` -- harvested session examples (when present). Data that must not be stored in this skill or its references: - Secrets, credentials, tokens. - Machine-specific filesystem paths (`/home/...`, `/Users/...`, `~/ai/...`). The validator (`MACHINE_PATH_LEAK`) flags these as HIGH. - Private URLs, customer data, or unredacted personal information. ### Coverage matrix | Dimension | Status | Evidence | |---|---|---| | Trigger fixtures | complete | distillery/tests/fixtures/triggers/ia-terraform.jsonl (>=5 should_trigger, >=5 should_not_trigger) | | Hook regex pattern | complete | plugins/whetstone/hooks/skill-patterns.sh (`SKILL_PATTERNS[ia-terraform]`) | | Reference architecture | n/a | no references; SKILL.md is self-contained | | Real-usage signal | <!-- populated by harvest-sessions when sessions exist --> | distillery/.eval-data/ia-terraform/ (created by harvest-sessions) | ## Evaluation Lightweight (run on every change): ```bash python3 distillery/scripts/distiller.py validate-plugin --component ia-terraform python3 distillery/scripts/distiller.py test-triggers --skill ia-terraform ``` Deeper (when behavior risk warrants): ```bash python3 distillery/scripts/distiller.py dspy-eval ia-terraform python3 distillery/scripts/distiller.py diagnose-negatives ia-terraform ``` Acceptance gates: - `validate-plugin --component ia-terraform` returns 0 HIGH findings. - `test-triggers --skill ia-terraform` returns F1 = 1.0 with floors of 5 should_trigger and 5 should_not_trigger. - For dspy-eval, the composite score does not regress against the most recent saved baseline (see `distillery/.eval-data/ia-terraform/history.json`). ## Known Limitations <!-- to fill in over time as drift surfaces. Default rule: any time diagnose-negatives surfaces a recurring failure pattern, document it here so future maintainers understand the trade-off the current implementation accepts. --> ## Maintenance Notes - Update `SKILL.md` when the runtime workflow, branch conditions, or output contract changes. - Update this `SPEC.md` when intent, scope, evidence model, evaluation gates, or maintenance expectations change. - Update the trigger fixture when adding new positive phrasings, removing stale ones, or expanding scope (the 5/5 floor is a hard validator gate). - Update the hook regex in `skill-patterns.sh` whenever fixture positives expose a missed phrasing; verify F1 = 1.0 with `eval-triggers` before committing. - Run the full release pipeline via `/release` -- never bump versions or update CHANGELOG.md from a per-skill edit.
Comments (0)
Sign in to join the conversation.
Reviews (0)
No reviews yet.
No comments yet.