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

Remote State & Backends

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

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

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

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

Local state (default) က laptop တစ်လုံးတည်းမှာပဲ ရှိပါတယ် — team member တခြားက state ကို မမြင်နိုင်ပါဘူး, ၂ ယောက်တစ်ပြိုင်နက် apply လုပ်ရင် state file ကွဲသွားနိုင်ပါတယ်။ Remote Backend (`backend "s3" { ... }`) က state file ကို shared location (S3 bucket, Terraform Cloud) မှာ သိမ်းပေးပြီး, team member အားလုံးက state အသစ်ဆုံးကို အမြဲ မြင်နိုင်ပါတယ်။ State Locking (DynamoDB table ကို S3 backend နဲ့ တွဲသုံးလေ့ရှိ) က တစ်ယောက် apply လုပ်နေချိန် နောက်တစ်ယောက် တစ်ပြိုင်နက် apply မလုပ်နိုင်အောင် lock ချထားပေးပါတယ် — state corruption ကို ကာကွယ်ပါတယ်.

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

`terraform { backend "s3" { bucket = "my-terraform-state"; key = "prod/terraform.tfstate"; region = "us-east-1"; dynamodb_table = "terraform-locks" } }` လို့ configure လုပ်ရင်, `terraform init` run တဲ့အခါ state ကို local ကနေ S3 ဆီ migrate လုပ်ပေးပါတယ် — အဲဒီနောက်ပိုင်း team member ဘယ်သူမဆို same config folder ကနေ `terraform apply` run ရင် state အသစ်ဆုံးကို အမြဲ ရရှိပါလိမ့်မယ်.

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

hcl
terraform {
  backend "s3" {
    bucket         = "my-terraform-state"
    key            = "prod/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "terraform-locks"
    encrypt        = true
  }
}
You should see
$ terraform init
Initializing the backend...

Successfully configured the backend "s3"! Terraform will
automatically use this backend unless the backend configuration
changes.

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

Remote backend config (S3 ဒါမှမဟုတ် သင်ရင်းနှီးတဲ့ provider) ကို ရေးကြည့်ပြီး, backend ကို configure လုပ်တဲ့အခါ Terraform ရဲ့ prompt/output ကို ကိုယ်တိုင် လေ့လာကြည့်ပါ (တကယ်တမ်း cloud resource create မလုပ်ပါနှင့်, dry-run/documentation review ပဲ လုပ်ပါ)။

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

S3 bucket (state storage) ကို public access ပြုလုပ်မိရင် sensitive data အားလုံး (password, secret) leak ဖြစ်နိုင်ပါတယ် — bucket ကို private ထားပြီး encryption enable လုပ်ထားပါ။

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

  • Remote backend setup မလုပ်ဘဲ team member အများနဲ့ local state file ကို git commit ဖြင့် share ကြိုးစားခြင်း — merge conflict ဖြစ်တတ်ပြီး state corrupt ဖြစ်နိုင်ပါတယ်
  • State locking (DynamoDB table) ကို setup မလုပ်ဘဲ S3 backend ကိုပဲ သုံးခြင်း — locking မရှိရင် concurrent apply ကြောင့် state ပျက်စီးနိုင်ပါတယ်

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

Remote backend config (S3 ဒါမှမဟုတ် သင်ရင်းနှီးတဲ့ provider) ကို ရေးကြည့်ပြီး, backend ကို configure လုပ်တဲ့အခါ Terraform ရဲ့ prompt/output ကို ကိုယ်တိုင် လေ့လာကြည့်ပါ (တကယ်တမ်း cloud resource create မလုပ်ပါနှင့်, dry-run/documentation review ပဲ လုပ်ပါ)။

You'll know it worked when: $ terraform init Initializing the backend... Successfully configured the backend "s3"! Terraform will automatically use this backend unless the backend configuration changes.

Remote State & Backends | Thuta Learning