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

HCL Syntax Basics

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

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

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

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

HCL syntax က JSON/YAML ထက် ဖတ်ရလွယ်အောင် ဒီဇိုင်းလုပ်ထားပါတယ် — `block_type "label" { argument = value }` ဆိုတဲ့ pattern ကို အထပ်ထပ် တွေ့ရပါလိမ့်မယ်။ Block type (resource, provider, variable, output) တွေက Terraform ရဲ့ 'keyword' တွေပါ။ Argument က `key = value` pattern ပါ — value က string, number, boolean, list, map ဖြစ်နိုင်ပါတယ်။ Expression ကတော့ `${...}` (modern HCL မှာ ချန်ထားလို့ရတဲ့) reference syntax ဖြင့် value တစ်ခုချင်းစီကို တခြား resource ရဲ့ output နဲ့ ချိတ်ဆက်ခြင်း ပါ။

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

`resource "local_file" "greeting" { filename = "hello.txt"; content = "Hello, Terraform!" }` ဆိုတဲ့ code က 'local_file type ရဲ့ resource တစ်ခု၊ ကိုယ့် config ထဲမှာ greeting လို့ ခေါ်မယ်၊ filename နဲ့ content argument ၂ ခုပါ' လို့ ဖတ်ရပါတယ်။ Comment ကို `#` (ဒါမှမဟုတ် `//`) နဲ့ ရေးလို့ရပြီး, multi-line string ကို heredoc syntax (`<<EOF ... EOF`) နဲ့ ရေးလို့ရပါတယ်.

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

hcl
# This is a comment
resource "local_file" "greeting" {
  filename = "hello.txt"
  content  = "Hello, Terraform!"
}

variable "environment" {
  type    = string
  default = "dev"
}

locals {
  tags = {
    project = "tutorial"
    owner   = "student"
  }
}
You should see
HCL block/argument/expression syntax ကို ဖတ်ရှင်းနိုင်မည်။

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

အထက်က HCL code ကို ဖတ်ပြီး, block type ၃ ခု (resource, variable, locals) ရဲ့ label/argument ဘာတွေလဲ ကိုယ်တိုင် ခွဲခြားရေးကြည့်ပါ။

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

HCL file extension က `.tf` ပါ — folder ထဲက `.tf` file အားလုံးကို Terraform က တစ်ခါတည်း (တစ်ဖိုင်ချင်းစီ သီးခြား run တာ မဟုတ်ဘဲ) ပေါင်းပြီး load ပါတယ်။

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

  • HCL ကို YAML/JSON နဲ့ syntax တူတယ်လို့ ထင်ပြီး indentation-sensitive ဖြစ်တယ်လို့ ထင်ခြင်း — HCL က `{ }` brace-based syntax ပါ, indentation က style ပဲ, requirement မဟုတ်ပါ
  • String value ကို quote (`"..."`) မတပ်ဘဲ ရေးမိခြင်း (error ဖြစ်ပါလိမ့်မယ်)

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

အထက်က HCL code ကို ဖတ်ပြီး, block type ၃ ခု (resource, variable, locals) ရဲ့ label/argument ဘာတွေလဲ ကိုယ်တိုင် ခွဲခြားရေးကြည့်ပါ။

You'll know it worked when: HCL block/argument/expression syntax ကို ဖတ်ရှင်းနိုင်မည်။

HCL Syntax Basics | Thuta Learning