متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 161IaC · Terraform

How to find drift between Terraform and the cloud

Someone opened a port in the console "just for a test", an autoscaler changed a count, a policy added tags — now the cloud is different from your Terraform code. That is drift. Undetected, the next apply silently undoes urgent fixes or fails halfway. This how-to finds drift with a refresh-only plan, runs the check on a schedule, and decides per case: put the change into code, or let Terraform set it back.

  • 5 الدروس
  • 3 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

161.1

The quick answer

terraform plan -refresh-only
terraform plan -detailed-exitcode -lock=false
echo $?

Exit code of -detailed-exitcode: 0 = no changes, 2 = changes (drift or new code), 1 = error.

161.2

Step by step

  1. 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"], … },
  1. 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 stayPut it into the code, then plan shows no changes.
Made on purpose by another system (autoscaling, tag policy)lifecycle { ignore_changes = [...] } for that attribute.
  1. Accept the real values into state without changing resources (rarely needed — prefer fixing the code):
terraform apply -refresh-only
  1. Ignore attributes another system owns:
asg.tf (part)
resource "aws_autoscaling_group" "web" {
  desired_capacity = 2
  lifecycle {
    ignore_changes = [desired_capacity]
  }
}
  1. Check on a schedule — a nightly CI job per environment (read-only credentials), alert on exit code 2:
.github/workflows/drift.yml (part)
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=false

A failed (exit 2) run = drift to look at.

  1. 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.

الأوامر في هذا الدرس

161.3

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.

الأوامر في هذا الدرس

161.4

Common problems

ProblemCauseFix
Drift every night on the same attributeAnother system changes it on purpose.ignore_changes for that attribute.
apply undid an urgent manual fixDrift not seen before apply.Always read the plan; put hotfixes into code right away.
Drift job takes the state lockPlan locks by default.-lock=false for read-only checks.
Resource deleted outside TerraformPlan wants to create it again.Decide: re-create (apply) or remove from code/state (removed block).
Huge refresh is slow / API rate limitsVery large states.Split states; refresh per state on a schedule.
Nobody knows who made the changeNo audit trail checked.CloudTrail / Azure Activity Log for that resource.

161.5

Go further

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.

كل الأوامر في هذا الفصل