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

Project — Multi-Environment Setup

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

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

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

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

Real project တွေမှာ dev environment က instance size သေးသေး/cheap, production environment က instance size ကြီးကြီး/redundant ဖြစ်ရလေ့ရှိပါတယ် — Module (Project 1) ကို base template အဖြစ်သုံးပြီး, environment တစ်ခုစီအတွက် `dev.tfvars`, `staging.tfvars`, `production.tfvars` ဆိုပြီး variable value ခွဲထားတဲ့ pattern ကို အသုံးများပါတယ်။ `terraform apply -var-file="production.tfvars"` ဆိုတာမျိုးနဲ့ environment အလိုက် value set ကို ရွေးချယ် apply လုပ်ပါတယ် — code (module) တစ်ခုတည်းကို environment ၃ ခုစလုံးက share သုံးနေတာမို့, code duplicate ဖြစ်စရာ မလိုပါ.

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

`environments/dev.tfvars` ထဲမှာ `instance_type = "t3.micro"`, `instance_count = 1`, `environments/production.tfvars` ထဲမှာ `instance_type = "t3.large"`, `instance_count = 3` ဆိုပြီး ခွဲထားပါ — root config (module ခေါ်သုံးထားတဲ့) တစ်ခုတည်းကိုပဲ `-var-file` flag ကွဲပြားစွာ ပေးပြီး environment အလိုက် apply လုပ်ပါတယ်။ Remote backend (Advanced chapter) ကိုပါ environment အလိုက် key ခွဲထား (`key = "dev/terraform.tfstate"` vs `key = "prod/terraform.tfstate"`) သင့်ပါတယ်.

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

bash
# environments/dev.tfvars
# instance_type = "t3.micro"
# instance_count = 1

# environments/production.tfvars
# instance_type = "t3.large"
# instance_count = 3

terraform apply -var-file="environments/dev.tfvars"
terraform apply -var-file="environments/production.tfvars"
You should see
$ terraform apply -var-file="environments/dev.tfvars"
  + instance_type = "t3.micro"
  + instance_count = 1
Plan: 1 to add, 0 to change, 0 to destroy.

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

`dev.tfvars`/`production.tfvars` file ၂ ခု ကိုယ်တိုင်ရေးပြီး, `local_file` resource (content ကို variable ဖြင့်) ကို `-var-file` flag ကွဲပြားစွာ ပေးကာ ၂ ကြိမ် apply ကြည့်ပါ — output file ၂ ခုရဲ့ content ကွဲပြားနေတာ confirm လုပ်ကြည့်ပါ။

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

Production `.tfvars` file ကို apply လုပ်ခင် environment name ကို terminal ပေါ်မှာ double-check လုပ်ပါ — dev/production ကို မှားပြီး apply လုပ်မိတာက production incident ရဲ့ အဖြစ်များဆုံး root cause တစ်ခုပါ။

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

  • Environment အားလုံးအတွက် state file/key တစ်ခုတည်းကို share ခြင်း — dev apply လုပ်ရင်း production resource ကို accidental ထိခိုက်စေနိုင်ပါတယ်
  • `-var-file` flag ကို မေ့ပြီး default value အတိုင်း apply လုပ်မိခြင်း (wrong environment ပေါ် apply ဖြစ်သွားနိုင်ပါတယ်)

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

`dev.tfvars`/`production.tfvars` file ၂ ခု ကိုယ်တိုင်ရေးပြီး, `local_file` resource (content ကို variable ဖြင့်) ကို `-var-file` flag ကွဲပြားစွာ ပေးကာ ၂ ကြိမ် apply ကြည့်ပါ — output file ၂ ခုရဲ့ content ကွဲပြားနေတာ confirm လုပ်ကြည့်ပါ။

You'll know it worked when: $ terraform apply -var-file="environments/dev.tfvars" + instance_type = "t3.micro" + instance_count = 1 Plan: 1 to add, 0 to change, 0 to destroy.

Project — Multi-Environment Setup | Thuta Learning