A CLI tool for drift detection and destructive-change gating in Terraform/OpenTofu plans.
- Detects drift using refresh-only plans (OpenTofu preferred, Terraform fallback)
- Parses plan JSON and counts updates / deletes / replaces
- Outputs Markdown (default), text, or json summary (counts + resource addresses)
- Flags:
--path(default.),--format md|text|json(defaultmd),--strict(exit code 2 if drift detected) - Gate subcommand to enforce destructive-change policy (delete/replace) for normal plan JSON
- Works with only Terraform or only OpenTofu installed
- CI-ready with strict exit codes
Copy-paste friendly. Replace
.with your IaC directory if needed.
# With Go installed (Go 1.22+):
go install github.com/ha36d/drift-checker@latest
# Or build from the repo root:
go build -o drift-checker .# In your IaC directory
tofu init || terraform initYou don’t need to run this manually for drift-checker to work—drift-checker generates and reads a plan internally.
If you want to sanity-check your runner first:
# OpenTofu (preferred if available)
tofu plan -refresh-only -out=drift-checker.plan
tofu show -json drift-checker.plan | jq . > /dev/null
# Terraform (fallback)
terraform plan -refresh-only -out=drift-checker.plan
terraform show -json drift-checker.plan | jq . > /dev/null# Markdown output (default), exits 0 if clean; use --strict to exit 2 on drift
drift-checker scan --path . --strictAlternative formats:
# Plain text
drift-checker scan --path . --format text
# Machine-readable JSON (stdout is **only** the JSON)
drift-checker scan --path . --format jsonUse gate to enforce a policy on destructive actions (deletes/replaces) from a standard plan JSON (output of show -json, not refresh-only).
# Fail CI if any delete/replace appears
drift-checker gate --input plan.json --strict
# Fail if deletes or replaces exceed thresholds
drift-checker gate --input plan.json --strict --max-deletes 0 --max-replaces 0
# Output formats: md|text|json; include a list of destructive addresses
drift-checker gate --input plan.json --format json --listdrift-checker gate uses standard shell exit codes so you can gate CI jobs.
| Scenario | Exit code |
|---|---|
| Safe (no destructive and thresholds not exceeded) | 0 |
Destructive present or thresholds exceeded (--strict) |
2 |
| Any other error (I/O, invalid JSON, etc.) | 1 |
Tip: Use
--strictin CI to fail the job on destructive changes: exit code2clearly distinguishes policy violations from other errors.
{
"updates": 0,
"replaces": 0,
"deletes": 0,
"destructive_total": 0,
"destructive": ["module.db.aws_db_instance.main"],
"total": 0
}destructiveis included only with--list.destructive_total=replaces + deletes.totalequals the count of all changed resource addresses in the plan JSON (updates + deletes + replaces).
We look at resource_changes[*].change.actions:
["delete"]→ destructive["create","delete"]or["delete","create"]→ replace → destructive["update"]→ non-destructive
# In your IaC directory
tofu init || terraform init
# Drift detection
drift-checker scan --path . --strict
# Gate using a normal plan JSON you've already generated
terraform show -json drift-checker.plan > plan.json
drift-checker gate --input plan.json --strict --format md --listscanpreferstofu, falls back toterraform, and uses a reliable two-step JSON flow.gatedoes not run a plan: it reads a provided plan JSON (normalshow -jsonfile).- All logs are written to stderr; stdout is reserved for the user-selected output (Markdown/Text/JSON).
-
cmd/– CLI commands (Cobra)scan.go– refresh-only scangate.go– destructive-change policy gate
-
internal/plan/– Runner selection, plan execution, and JSON parsing -
internal/report/– Output formatting for scan summaries -
internal/drift/– Orchestration for scan
These tests reuse your existing fixtures and patterns:
plan_drift.json(has 1 update, 1 delete, 1 replace) → triggers exit code2in strict mode.- New
plan_updates_only.json→ safe (exit0even with--strict). - Threshold test sets
--max-deletes 0 --max-replaces 0to trip exit2.
You can run them with your current CI (go test -v ./...).
Apache-2.0