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

Workspaces

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

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

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

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

Module (ယခင် lesson) က code reuse အတွက်ဖြစ်ပြီး, Workspace ကတော့ state isolation အတွက်ပါ — config folder တစ်ခုတည်းနေရာမှာပဲ `terraform workspace new dev`, `terraform workspace new prod` ဆိုပြီး workspace ၂ ခု create လုပ်ရင်, state file ကို workspace အလိုက် (`terraform.tfstate.d/dev/`, `terraform.tfstate.d/prod/`) ခွဲသိမ်းပေးပါတယ် — dev workspace မှာ apply လုပ်တာက prod workspace ရဲ့ resource ကို လုံးဝ မထိခိုက်ပါ။ `terraform.workspace` ဆိုတဲ့ built-in variable ကို config ထဲမှာ ကိုယ်တိုင် reference လုပ်ပြီး, workspace အလိုက် value ပြောင်းလို့လည်း ရပါတယ်.

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

`terraform workspace new staging` → `terraform workspace select staging` → `terraform apply` ဆိုတဲ့ sequence နဲ့ staging environment ကို default environment နဲ့ ခွဲထားပြီး run လို့ရပါတယ် — `instance_type = terraform.workspace == "prod" ? "t3.large" : "t3.micro"` ဆိုတဲ့ expression နဲ့ workspace အလိုက် value ကွဲပြားစွာ ချထားလို့လည်း ရပါတယ်.

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

bash
# Create and switch to a new workspace
terraform workspace new staging

# List all workspaces
terraform workspace list

# Switch back to default
terraform workspace select default

# See which workspace is currently active
terraform workspace show
You should see
$ terraform workspace list
  default
* staging
  production

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

`terraform workspace new dev` နဲ့ workspace အသစ်တစ်ခု create လုပ်ပြီး, `local_file` resource (Basic chapter) ကို apply ကြည့်ပါ — `default` workspace ကို ပြန် select လုပ်ပြီး state ခွဲနေတာကို confirm လုပ်ကြည့်ပါ။

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

Workspace တွေအားလုံးက backend config (state storage location) တစ်ခုတည်းကို share ပါတယ် — access control ကို workspace level မှာ ခွဲမရပါ (production workspace ကို developer အားလုံး access ရနေနိုင်ပါတယ်)။

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

  • Workspace ကို module ရဲ့ အစားထိုး (environment ခွဲဖို့ နည်းလမ်းတစ်ခုတည်း) လို့ ထင်ခြင်း — team အများစုက module + separate root config ကို ပိုသုံးကြပါတယ် (workspace ကို light-weight case (feature branch testing) အတွက်ပဲ သုံးလေ့ရှိ)
  • Workspace ပြောင်းထားတာ မေ့ပြီး wrong workspace ပေါ်မှာ apply လုပ်မိခြင်း — `terraform workspace show` နဲ့ အမြဲ confirm လုပ်ပါ

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

`terraform workspace new dev` နဲ့ workspace အသစ်တစ်ခု create လုပ်ပြီး, `local_file` resource (Basic chapter) ကို apply ကြည့်ပါ — `default` workspace ကို ပြန် select လုပ်ပြီး state ခွဲနေတာကို confirm လုပ်ကြည့်ပါ။

You'll know it worked when: $ terraform workspace list default * staging production

Workspaces | Thuta Learning