What is Terraform state drift and how do you handle it?
State drift occurs when the real infrastructure differs from what Terraform state believes it to be — typically due to manual changes made in the AWS console or another tool.
Detection: terraform plan will show changes that seem unexpected.
Resolution options:
- Import:
terraform importto import manually created resources into state. - Refresh:
terraform refreshto update state to match reality (deprecated in favor ofplan -refresh-only). - Accept drift: Use
lifecycle { ignore_changes = [...] }for intentionally externally-managed attributes.
Prevention: Forbid all manual console access to production environments using IAM SCPs.