Run terraform plan -refresh-only -detailed-exitcode to detect infrastructure drift in CI

domain: developer.hashicorp.com/terraform · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Configure backend credentials and run `terraform init` in your CI job before any plan step.
  2. Execute `terraform plan -refresh-only -detailed-exitcode -input=false -out=tfplan`; exit code 0 means no drift, exit code 1 means error, exit code 2 means drift detected.
  3. In your CI script, capture the exit code with `EXIT_CODE=$?` immediately after the plan command; check `[ $EXIT_CODE -eq 2 ]` to branch on drift.
  4. On drift, fail the pipeline or open an alert; optionally run `terraform show -json tfplan` to emit a structured drift report as a pipeline artifact.
  5. For scheduled drift checks, add a cron-triggered CI job that runs the refresh-only plan and posts results to a Slack webhook or incident channel.
  6. Avoid `terraform apply` in the refresh-only flow; this plan variant only refreshes state and never proposes changes.

Known gotchas

Related routes

Enable HCP Terraform workspace health assessments for drift detection and continuous validation
developer.hashicorp.com/terraform/cloud-docs · 6 steps · unrated
Detect and remediate drift in an AWS CloudFormation stack using the detect-stack-drift API
docs.aws.amazon.com/AWSCloudFormation · 6 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans