Interview › Infrastructure as Code (Terraform, Ansible)
What is configuration drift, and how does Terraform detect it?
Infrastructure as Code (Terraform, Ansible) · Basic level
Answer
Configuration drift is when real infrastructure no longer matches Terraform code or state, usually because of manual changes, external automation, or provider-side defaults. Terraform detects drift during refresh in plan/apply by reading current remote objects and comparing them with state and desired configuration.
Technical explanation
Drift can appear as an in-place update, replacement, deletion, or state-only change in the plan.
Some drift is intentional, for example autoscaling capacity, and should be handled with ownership boundaries or ignore_changes.
Regular drift detection reduces surprises during emergency applies.
Keep Terraform's ownership boundary clear: one state should own a resource or field, and other tools should consume published outputs instead of modifying it.
Use fmt, validate, linting, policy checks, plan review, and state locking before production applies.
Design for small blast radius by splitting state around lifecycle, permissions, and recovery boundaries.
Hands-on example
1. Set up drift handling for: What is configuration drift, and how does Terraform detect it?
2. Schedule a read-only plan job per workspace:
terraform init
terraform plan -detailed-exitcode -out=drift.tfplan || status=$?
terraform show -json drift.tfplan > drift.json3. Interpret exit code 0 as no drift, 2 as changes present, and 1 as an error requiring investigation.
4. For state-only synchronization, use refresh-only review:
terraform plan -refresh-only -out=refresh.tfplan
terraform apply refresh.tfplan5. Open a ticket that classifies drift as revert, codify, ignore because externally owned, or remove from Terraform ownership.
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Infrastructure as Code (Terraform, Ansible) interview questions
- What is terraform import, and when do you need it?
- How do you handle resources changed manually outside Terraform?
- What is the difference between a data source and a resource?
- What are Terraform workspaces, and what are their limitations for environments?
- What is a dynamic block, and when would you use it?
- What is the difference between using workspaces and separate state files per environment?
- What problem does for_each solve that count creates when removing items?
- How do you structure Terraform code for multiple environments (dev/staging/prod)?