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

ပထမဆုံး Resource ဖန်တီးခြင်း

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

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

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

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

Resource ဆိုတာ Terraform က create/manage လုပ်ပေးမယ့် 'အရာဝတ္ထု' တစ်ခုချင်းစီကို ကိုယ်စားပြုတာပါ — server, database, file, DNS record — resource type အားလုံးက ဒီ concept အောက်ကနေ လာပါတယ်။ `resource "<type>" "<local_name>"` ဆိုတဲ့ syntax ကို သုံးပြီး, block ထဲမှာ argument (config detail) ရေးထည့်ပါတယ်။ Terraform က ဒီ resource ကို state file ထဲ 'ငါ ဖန်တီးပြီးသား' လို့ မှတ်ထားပြီး, နောက်တစ်ကြိမ် `apply` ထပ် run ရင် (change မရှိရင်) ဘာမှ ထပ်မလုပ်တော့ပါ.

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

`terraform apply` ပြီးရင် `hello.txt` ဆိုတဲ့ file တကယ် disk ပေါ်မှာ ပေါ်လာမှာပါ — content ကို ပြင်ပြီး `apply` ထပ်run ရင် Terraform က 'ပြောင်းလဲမှုရှိတယ်' သိပြီး file ကို update ပေးပါလိမ့်မယ်။ `terraform destroy` run ရင် Terraform-managed resource အားလုံးကို ဖျက်ပေးပါတယ် — practice ပြီးတိုင်း cleanup လုပ်ဖို့ ဒီ command က အရေးကြီးပါတယ်.

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

hcl
terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.5"
    }
  }
}

resource "local_file" "greeting" {
  filename = "${path.module}/hello.txt"
  content  = "Hello, Terraform!"
}
You should see
$ terraform apply
local_file.greeting: Creating...
local_file.greeting: Creation complete after 0s

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

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

အထက်က code ကို file အနေနဲ့ save လုပ်ပြီး `terraform init && terraform apply` run ကြည့်ပါ — `hello.txt` ပေါ်လာတာ confirm လုပ်ပြီးရင် `terraform destroy` နဲ့ ရှင်းပါ။

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

`terraform destroy` က resource ကို 'ပြန်ယူလို့မရအောင်' ဖျက်ပေးပါတယ် — production state ပေါ်မှာ destroy command ကို အလွန် သတိထားပြီးမှ run ပါ။

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

  • `terraform init` မလုပ်ဘဲ `terraform apply` ကို တိုက်ရိုက် run ခြင်း — provider plugin မရှိသေးလို့ error ဖြစ်ပါလိမ့်မယ်
  • Resource ကို manual (Terraform ပြင်ပက) ပြင်ပြီးရင် Terraform က ဒါကို 'drift' အဖြစ် မှတ်ထားမည်ဆိုတာ မသိခြင်း (နောက် chapter drift lesson မှာ ပိုနက်နက်ရှိုင်းရှိုင်း ကြည့်မယ်)

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

အထက်က code ကို file အနေနဲ့ save လုပ်ပြီး `terraform init && terraform apply` run ကြည့်ပါ — `hello.txt` ပေါ်လာတာ confirm လုပ်ပြီးရင် `terraform destroy` နဲ့ ရှင်းပါ။

You'll know it worked when: $ terraform apply local_file.greeting: Creating... local_file.greeting: Creation complete after 0s Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

ပထမဆုံး Resource ဖန်တီးခြင်း | Thuta Learning