Files
magnus919_agent-skills/terraform/references/07-diagnostics.md
T
Magnus HedemarkGitHubfactory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
00abbf90a4 feat(skill): add Terraform operational skill (#263)
* feat(skill): add Terraform operational skill

Add a single tool skill for Terraform and OpenTofu operations: module
structure, state backends and locking, plan/apply workflow, drift detection,
remote state, upgrade and refactor flows, and evidence-based diagnostics.
Ships the agent-first tfops wrapper (JSON output, direct state-file analysis,
--dry-run/--yes/--force mutation gate), a fixture-tested suite, six eval
cases, dated references, and routing up to platform-engineering. Closes #243.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>

* fix(skill): clarify missing-binary report in tfops doctor

When the TERRAFORM env override names a binary that cannot be found,
doctor now reports the env value with a (not found) marker instead of
falling back to the generic default name.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>

---------

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
2026-08-03 17:38:46 -04:00

3.2 KiB

Diagnostics

Diagnose in evidence order: binary/version → config validation → backend + lock status → state serial/lineage → plan diff → apply error → boundary check. Each layer narrows the next; skipping layers produces guesswork.

First-response diagnostic (tfops)

scripts/tfops doctor --json                 # binary, version, config files, backend hint, optional state summary
scripts/tfops plan --state state.json --json    # state-level inventory without a binary
scripts/tfops plan --json                   # real plan against the backend (needs binary + backend access)

doctor tells you whether the failure is environmental (no binary, no config) or state-related before you interpret any deeper error.

Failure patterns

Lock errors (Error acquiring the state lock)

  • Find the holder through backend-specific inspection (the DynamoDB lock item, the cloud workspace run, the Consul session).
  • Confirm no operation is genuinely running; only then force-unlock <LOCK_ID> with the ID from the error.
  • Never delete the lock row blindly; never bypass locking by switching to the local backend to run a command.

Serial/lineage mismatches (Error: state file in path does not match the given serial or lineage errors)

  • The local/backup state and the live state diverged. Use state pull to see the live state, compare serials, and restore from a known-good backup after review — never overwrite a newer serial.

terraform init backend errors

  • Verify backend configuration, credentials/identity, and network path. -reconfigure re-reads the backend block; a changed backend without -migrate-state/-reconfigure is the usual trigger.

Plan/apply errors mid-run

  • Read the resource-level error, not just the summary; providers give actionable messages (API errors, quota, IAM).
  • A partial apply leaves state consistent but the resource may not exist: re-plan to see what remains, then apply again.
  • Timeout or "context deadline exceeded": check the provider's operation timeout and the backend/network; bounded retries beat immediate blind re-apply.

Validate errors

  • terraform validate diagnostics point at file/line; fix config errors before planning. Tainted resources (tfops reports them) are not a validate error — they are a plan/apply concern.

Verification boundary

Diagnosis Minimum evidence
Binary healthy terraform version / tofu version exit 0
Config valid validate exit 0 with no diagnostics
Backend reachable init exit 0; lock acquired and released
State intact tfops state --state FILE --json parses; serial/lineage match backend
Root cause fixed The originally failing operation succeeds, then a clean re-plan

Sources

Last Updated: 2026-08-03