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

Terraform State

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

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

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

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

State file (`terraform.tfstate`) ထဲမှာ resource တစ်ခုချင်းစီရဲ့ current attribute အားလုံး (ID, IP address, tag) ကို JSON format နဲ့ မှတ်ထားပါတယ် — Terraform run တိုင်း configuration (ငါလိုချင်တာ) ကို state (ငါဘာရှိပြီးသားလဲ) နဲ့ diff ယူပြီး ဘာလုပ်ရမလဲ ဆုံးဖြတ်ပါတယ်။ State file ပျောက်သွားရင် Terraform က resource တွေ ရှိနေတယ်ဆိုတာတောင် 'မသိတော့' ပါဘူး — ထပ်ပြီး create ဖို့ ကြိုးစားနိုင်ပါတယ် (duplicate resource ဖြစ်နိုင်ပါတယ်)။ `terraform state list` (resource list ကြည့်ခြင်း), `terraform state show <resource>` (attribute detail ကြည့်ခြင်း) command တွေက state ကို inspect လုပ်ဖို့ အသုံးဝင်ပါတယ်.

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

Team ၂ ယောက် (local laptop ၂ လုံး) က state file ကို local disk မှာပဲ သိမ်းထားရင် — တစ်ယောက်က apply လုပ်ပြီးရင် နောက်တစ်ယောက်ရဲ့ state က stale (updated မဖြစ်) ဖြစ်နေနိုင်ပါတယ် — conflict/duplicate resource ဖြစ်နိုင်ပါတယ် (ဒါကြောင့် production မှာ Remote State (နောက် chapter) ကို သုံးရပါတယ်)။

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

bash
# List all resources Terraform is tracking
terraform state list

# Show full detail of one resource
terraform state show aws_instance.web

# See the raw state file (JSON) — read-only inspection
cat terraform.tfstate
You should see
$ terraform state list
aws_instance.web
local_file.greeting

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

First-resource lesson ရဲ့ local_file resource ကို apply ထားထားပြီးဆိုရင် `terraform state list` နဲ့ `terraform state show local_file.greeting` ကို run ကြည့်ပါ — state ထဲက attribute တွေကို ကိုယ်တိုင် ဖတ်ကြည့်ပါ။

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

State file ကို backup မထားဘဲ ဖျက်မိရင် (ဒါမှမဟုတ် corrupt ဖြစ်ရင်) production infrastructure ကို ပြန်လည် manage ရခက်ခဲသွားနိုင်ပါတယ် — Remote State + versioning ကို production မှာ အမြဲသုံးပါ။

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

  • State file ကို manual edit (text editor ဖြင့် တိုက်ရိုက်ပြင်) လုပ်ခြင်း — state format မှားသွားရင် Terraform ကိုယ်တိုင် အလုပ်မလုပ်နိုင်တော့ပါ (terraform state ရဲ့ dedicated command တွေကိုသာ သုံးသင့်)
  • Team member အများစု local state file ကို share drive/USB ကနေ manual ကူးသုံးခြင်း — conflict ဖြစ်တတ်ပါတယ်

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

First-resource lesson ရဲ့ local_file resource ကို apply ထားထားပြီးဆိုရင် `terraform state list` နဲ့ `terraform state show local_file.greeting` ကို run ကြည့်ပါ — state ထဲက attribute တွေကို ကိုယ်တိုင် ဖတ်ကြည့်ပါ။

You'll know it worked when: $ terraform state list aws_instance.web local_file.greeting

Terraform State | Thuta Learning