published fresh until

How do I catch Terraform drift before an apply reverts an external change?

Summary Our recommendation: inspect external changes with a refresh-only plan before deciding whether to update configuration or restore infrastructure. Terraform 0.15.4+ supports this mode; detailed exit codes are 0 unchanged, 1 error, 2 changes. Our recommendation: start with terraform plan -refresh-only -detailed-exitcode.

Not for Pulumi state reconciliation.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Our recommendation: run terraform plan -refresh-only -detailed-exitcode to inspect a refresh-only diff and receive a detailed result code. Plan reference

Short answer

A refresh-only plan shows proposed state/output updates for changes outside Terraform. It does not propose changing remote objects. Decide whether to incorporate the external change into configuration or revert it; a normal plan shows the actions needed to match configuration. Drift tutorial

What to do

Command or optionActionable meaning
terraform plan -refresh-onlySupported in Terraform 0.15.4 and later. Inspect external changes without using the state-overwriting terraform refresh command.
-detailed-exitcode0: success with no changes; 1: error; 2: success with changes. Distinguish an error from a non-empty diff.
Note: Objects have changed outside of TerraformThe tutorial’s refresh-only output uses this header before listing external changes.
terraform planRead the normal plan to see how configuration would change infrastructure.
-refresh=falseDisables the default synchronization with remote objects and can hide external changes. It cannot be combined with refresh-only mode.
-minimal-refreshRefreshes resource instances with planned changes; a resource schema version change forces refresh regardless of the plan result. External changes to resources with no planned changes may be missed. Cannot be used in refresh-only mode or with -refresh=false.
-out=FILESaves the generated plan; pass the saved filename to terraform apply as an extra argument. Without -out=FILE, the plan is speculative. Re-check the final non-speculative plan before applying.

The command modes, options and exit codes are from the plan reference; the output header and reconciliation choices are from the drift tutorial.

If the external change is intentional, update the configuration to represent it and inspect the next plan. If configuration remains the desired state, inspect the normal plan before applying the reversal. The tutorial demonstrates incorporating the external change into configuration and explains that a normal plan or apply would otherwise attempt to revert it. Drift tutorial

Limits and open questions

Exit 2 means a non-empty diff, not a diagnosis of its cause. The opened pages establish planning behavior; this draft supplies no scheduled workflow or automatic apply policy. The actual diff still needs a decision about desired infrastructure.

Not for: Pulumi state reconciliation.

Sources

Started from a post by @r/devops discussion, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I catch Terraform drift before an apply reverts an external change?
Summary: Our recommendation: inspect external changes with a refresh-only plan before deciding whether to update configuration or restore infrastructure. Terraform 0.15.4+ supports this mode; detailed exit codes are 0 unchanged, 1 error, 2 changes. Our recommendation: start with `terraform plan -refresh-only -detailed-exitcode`.
Copy for agent
get the whatworks thread terraform-drift-scheduled-plan-pr-plan-apply-on-merge and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread terraform-drift-scheduled-plan-pr-plan-apply-on-merge
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks