Prompt
How do I prevent configuration drift in Terraform state?
Latest observation
To prevent configuration drift in Terraform state, focus on keeping Terraform as the single source of truth and continuously detecting differences between code, state, and real infrastructure.
What causes drift
Drift happens when:
- Someone changes infrastructure manually in the cloud console or CLI
- External systems mutate resources
- Terraform state becomes stale or is modified incorrectly
- Multiple teams manage the same resource outside a clear ownership model
Best practices to prevent drift
1. Avoid manual changes
The most effective prevention is process-based:
- Treat Terraform as the only way to change managed infrastructure
- Restrict console/CLI write access where possible
- Use IAM/RBAC policies to limit out-of-band changes
- Use pull requests and reviews for all infra changes
2. Use remote state with locking
Store state in a shared backend that supports locking, such as:
- S3 + DynamoDB
- Terraform Cloud/Enterprise
- Azure Storage with blob leases
- GCS with state locking mechanisms
This prevents concurrent writes and accidental state corruption.
3. Run terraform plan regularly
Use automated plans in CI/CD:
- On every pull request
- On a schedule for drift detection
- Before every apply
A plan will show if real infrastructure differs from your configuration/state.
4. Automate drift detection
Set up scheduled jobs that run:
terraform plan -detailed-exitcode
Interpret exit codes:
0= no changes2= drift or changes detected1= error
Alert on non-zero results so drift is caught early.
5. Keep state and code in sync
- Commit Terraform code to version control
- Apply only reviewed code changes
- Avoid editing state manually
- Don’t import resources casually without documenting ownership
6. Use lifecycle rules carefully
Terraform lifecycle settings can help reduce noisy diffs, but use them intentionally:
ignore_changesfor fields managed externally or expected to varyprevent_destroyfor critical resources
Be careful: overusing ignore_changes can hide real drift.
7. Separate responsibilities
If another system must manage part of a resource:
- Split ownership cleanly
- Use separate resources/modules
- Avoid two tools managing the same attributes
For example, don’t let both Terraform and an operator/controller modify the same fields unless the provider supports that model.
8. Refresh state before applying
Modern Terraform automatically refreshes during plan/apply, but in workflows and scripts make sure you’re not using stale assumptions. Use plans generated from current remote state.
9. Use policy and governance
Enforce:
- Approval workflows
- Change management
- Policy-as-code (e.g. Sentinel, OPA/Conftest)
- Cloud audit alerts for manual modifications
10. Monitor and alert on cloud-side changes
Use cloud-native tools to detect unmanaged changes:
- AWS CloudTrail
- Azure Activity Log
- GCP Audit Logs
These don’t prevent drift, but they help detect it quickly.
Practical workflow
A good drift-resistant setup looks like this:
- Developers change Terraform code in Git
- CI runs
terraform fmt,validate, andplan - Approved changes are applied by automation
- Scheduled drift checks run
plan -detailed-exitcode - Alerts fire if drift is found
Important note
You can’t completely eliminate drift in dynamic environments, but you can:
- minimize manual changes
- detect drift quickly
- limit the blast radius
- restore desired state safely
If you want, I can also give you:
- a CI/CD example for drift detection
- a Terraform Cloud workflow
- or a script for scheduled drift checks