Detect and Fix Infrastructure Drift
Identify when real infrastructure has drifted from Terraform state and resolve discrepancies systematically.
Prerequisites
- -Terraform installed
- -Existing managed infrastructure
Steps
Refresh state to detect drift
Update the state file with the real-world status of all managed resources without making any changes.
The -refresh-only flag updates state without proposing any resource changes. Perfect for drift detection.
Apply the refresh to update state
Accept the refreshed state so Terraform knows the current real-world status of resources.
Review the refresh output carefully. This updates what Terraform considers the current state of your infrastructure.
Run a full plan to see pending changes
After refreshing, run a standard plan to see what changes Terraform would make to reconcile config with reality.
Review specific resource state
Inspect the state of a specific resource to understand its current attributes as Terraform sees them.
Decide: reconcile config or re-apply
Either update your .tf files to match the drifted state (accept drift) or apply the plan to enforce your config.
If the drift was intentional (e.g., emergency hotfix), update your config to match. If accidental, apply to enforce the desired state.
Full Script
FAQ
Discussion
Loading comments...