هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
The quick answer
Step by step
- See what changed outside Terraform — a refresh-only plan compares the state with the real resources and shows only that:
terraform plan -refresh-only
Note: Objects have changed outside of Terraform
# aws_security_group.web has changed
~ resource "aws_security_group" "web" {
~ ingress = [
+ { from_port = 22, to_port = 22, cidr_blocks = ["0.0.0.0/0"], … },- Decide per change:
| The change was… | Do |
|---|---|
| A mistake / not allowed (SSH open to the world) | Normal terraform apply — Terraform sets it back to the code. |
| Right and should stay | Put it into the code, then plan shows no changes. |
| Made on purpose by another system (autoscaling, tag policy) | lifecycle { ignore_changes = [...] } for that attribute. |
- Accept the real values into state without changing resources (rarely needed — prefer fixing the code):
terraform apply -refresh-only- Ignore attributes another system owns:
resource "aws_autoscaling_group" "web" {
desired_capacity = 2
lifecycle {
ignore_changes = [desired_capacity]
}
}- Check on a schedule — a nightly CI job per environment (read-only credentials), alert on exit code
2:
on:
schedule:
- cron: "17 3 * * *"
jobs:
drift:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v5
- run: terraform init -input=false
- run: terraform plan -refresh-only -detailed-exitcode -input=false -lock=falseA failed (exit 2) run = drift to look at.
- Stop drift at the source: read-only console access for most people, changes only through Terraform pipelines, cloud policies (SCP / Azure Policy) against risky manual changes, and CloudTrail/Activity Log to find who changed it.
Check that it worked
After you fixed code or reverted the change, terraform plan -detailed-exitcode returns 0 (no changes), and the nightly drift job is green.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| Drift every night on the same attribute | Another system changes it on purpose. | ignore_changes for that attribute. |
apply undid an urgent manual fix | Drift not seen before apply. | Always read the plan; put hotfixes into code right away. |
| Drift job takes the state lock | Plan locks by default. | -lock=false for read-only checks. |
| Resource deleted outside Terraform | Plan wants to create it again. | Decide: re-create (apply) or remove from code/state (removed block). |
| Huge refresh is slow / API rate limits | Very large states. | Split states; refresh per state on a schedule. |
| Nobody knows who made the change | No audit trail checked. | CloudTrail / Azure Activity Log for that resource. |
Go further
- Command pages: terraform, aws.
- Next how-tos: Plan and apply Terraform safely, See who did what in AWS with CloudTrail, Import an existing resource.
- Learn it: Terraform — Drift, lifecycle, Inspect state.
- Practise: Lab: work with state — inspect, move, remove, import and find drift.
1Which plan shows only changes made outside Terraform?
2What does exit code 2 of terraform plan -detailed-exitcode mean?
The plan has changes (drift or new code) — good for an alert in a scheduled job.