Terraform state is a snapshot. Reality moves on. The gap between what Terraform thinks exists and what actually exists is called drift, and every team running Terraform in production deals with it. This post covers how drift happens, how to detect it automatically, how to fix it when you find it, and how to prevent most of it in the first place.
What drift actually is
When you run terraform apply , Terraform writes the real-world state of every resource into the state file. From that moment, the state file starts going stale. Anything that changes infrastructure outside of Terraform creates drift: the state says one thing, the cloud says another.
A little drift is normal. A lot of drift is dangerous, because the next terraform apply will try to reconcile the difference, and that reconciliation can surprise you.…