Terraform · Infrastructure as code
How do you detect and handle infrastructure drift?
Short answer
Drift is the difference between real infrastructure and Terraform state. Detect it with scheduled plans or refresh-only runs, then decide whether to codify the manual change or revert it.
Drift happens when someone changes a resource outside Terraform, when another automation modifies it, or when a provider changes defaults. Terraform compares configuration, state and the refreshed remote object during plan, so a diff is the first signal.
terraform plan -refresh-only
terraform apply -refresh-only
terraform plan -detailed-exitcodeCauses and responses
- Console changes: either codify them or revert them through Terraform
- Other automation: define ownership and remove the competing writer
- Provider default changes: pin providers and review upgrade plans
- Resource deletion: decide whether to recreate or remove the block
The response depends on intent. If the manual change is correct, update the configuration and apply so the code matches. If it is wrong, apply the original configuration to restore it. Never use refresh-only as a substitute for fixing the configuration; it only updates state.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between a normal plan and a refresh-only plan?
- 2How do you decide whether to codify or revert drift?
- 3Why can pinning providers help with drift?
- 4How would you alert on drift in production?
Say it in the interview
Drift is any difference between what Terraform recorded, what the configuration says, and what actually exists. I detect it with scheduled plans or a refresh-only run, then decide whether the manual change should be codified or reverted. Refresh-only updates state but not configuration, so it is an inspection tool, not the fix. The alert needs a clear owner, otherwise drift reports get ignored.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Terraform mock interview and get scored feedback on depth, clarity and confidence.