ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
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"`) သင့်ပါတယ်.
အတူတူ ကြည့်မယ်
# 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"$ 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 တစ်ခုပါ။