Thuta Learning
ရှာဖွေရန်
AdvancedDevOpsintermediate

Import & Drift Detection

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Import & Drift Detection ကို ကြောက်စရာမလိုအောင် နားလည်မယ်
  • ကိုယ်တိုင် terraform command/HCL code ကို run ကြည့်တတ်မယ်
  • Real project ထဲမှာ ဒီ concept ကို ချက်ချင်း အသုံးချတတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

Company တစ်ခုမှာ Terraform မစသုံးခင် manual create ထားခဲ့တဲ့ resource (legacy infrastructure) ရှိတတ်ပါတယ် — ဒါတွေကို ဖျက်ပြီး Terraform နဲ့ အသစ်ပြန် create မယ်ဆိုရင် production downtime ဖြစ်နိုင်ပါတယ်။ `terraform import <resource_address> <existing_id>` က ရှိပြီးသား resource ကို ဖျက်/ပြန်ဖန်တီးစရာ မလိုဘဲ, state ထဲကို 'ဒီ resource ရှိပြီးသားပဲ' လို့ ထည့်ပေးပါတယ် (config block ကိုတော့ manual ရေးပေးရပါမယ်, import command က state ကို ဖြည့်ပေးရုံပါ)။ Drift ကတော့ Terraform state/config ထဲမှာ တစ်မျိုး, cloud provider ပေါ်မှာ တကယ်ရှိတာ (someone manual ပြင်ခဲ့တာ) တစ်မျိုး ကွဲသွားတဲ့ အခြေအနေကို ခေါ်ပါတယ် — `terraform plan` run ရင် drift ကို ရှာတွေ့ပြီး ပြပေးပါတယ်.

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Someone AWS console ကနေ instance_type ကို manual ပြောင်းလိုက်ရင် (t3.micro → t3.large), `terraform plan` run ရင် Terraform က 'config ထဲမှာ t3.micro ရေးထားတယ်, actual ကတော့ t3.large ဖြစ်နေတယ်' ဆိုတဲ့ drift ကို ရှာတွေ့ပြီး ပြပေးပါလိမ့်မယ် — `terraform apply` run ရင် Terraform config (t3.micro) ကို 'true source' အဖြစ်ယူပြီး actual state ကို ပြန်ညှိပေးပါလိမ့်မယ် (manual change ကို revert လုပ်တာနဲ့ တူပါတယ်).

အတူတူ ကြည့်မယ်

bash
# Import an existing S3 bucket into Terraform's state
terraform import aws_s3_bucket.legacy my-existing-bucket-name

# After import, check for drift
terraform plan

# If someone manually changed something in the console,
# plan will show the difference here
You should see
$ terraform import aws_s3_bucket.legacy my-existing-bucket-name
aws_s3_bucket.legacy: Importing from ID "my-existing-bucket-name"...
aws_s3_bucket.legacy: Import complete!

၅ မိနစ် စမ်းကြည့်

`terraform import` command ရဲ့ syntax (`terraform import <address> <id>`) ကို local_file resource တစ်ခု (manual file တစ်ခု ရှေ့ကတည်းက create ထားပြီး) နဲ့ ကိုယ်တိုင် စမ်း run ကြည့်ပါ။

သတိလေးတစ်ချက်

Drift detected ရင် `apply` ကို အလိုအလျောက် run မလုပ်ပါနှင့် — plan ကို သေချာဖတ်ပြီးမှ apply လုပ်ပါ, တစ်ခါတစ်ရံ manual change ကတော့ intentional (emergency fix) ဖြစ်နိုင်လို့ config ကို update လုပ်ရမှာလည်း ဖြစ်နိုင်ပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • `terraform import` ပြီးရင် config block (`resource "..." "..." { ... }`) ကို manual ရေးထည့်ဖို့ မေ့ခြင်း — import command က state ကိုပဲ ဖြည့်ပေးတာပါ, config ကို ကိုယ်တိုင် ရေးပေးရပါမယ်
  • Drift ကို regular ကြည့်နေဖို့ (`terraform plan` ကို scheduled job အနေနဲ့ run ဖို့) setup မလုပ်ဘဲ, manual change တွေ ကြာကြာ notice မရတော့ခြင်း

အခု ကိုယ်တိုင် စမ်းကြည့်

`terraform import` command ရဲ့ syntax (`terraform import <address> <id>`) ကို local_file resource တစ်ခု (manual file တစ်ခု ရှေ့ကတည်းက create ထားပြီး) နဲ့ ကိုယ်တိုင် စမ်း run ကြည့်ပါ။

You'll know it worked when: $ terraform import aws_s3_bucket.legacy my-existing-bucket-name aws_s3_bucket.legacy: Importing from ID "my-existing-bucket-name"... aws_s3_bucket.legacy: Import complete!

Import & Drift Detection | Thuta Learning