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

Dependencies & References

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

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

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

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

Resource A ရဲ့ argument ထဲမှာ Resource B ကို reference (`resource_b.id` ဆိုတာမျိုး) ရေးထားရင်, Terraform က ဒီ reference ကနေတစ်ဆင့် 'B ကို အရင် create ပြီးမှ A ကို create ရမယ်' ဆိုတာ automatic နားလည်ပါတယ် (Implicit Dependency) — dependency graph ကို Terraform ကိုယ်တိုင် တည်ဆောက်ပေးပါတယ်။ တစ်ခါတစ်ရံ resource နှစ်ခုက attribute reference တိုက်ရိုက် မရှိပေမယ့် logical order လိုအပ်တဲ့အခါ (ဥပမာ - IAM permission အရင် ရှိမှ resource create လို့ရမယ်ဆိုတာမျိုး) `depends_on = [resource_x]` ဆိုပြီး Explicit Dependency ကို manual ရေးပေးရပါတယ်.

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

`aws_instance` ရဲ့ `subnet_id = aws_subnet.app.id` လို့ ရေးထားရင် Terraform က subnet ကို instance ထက် အရင် create ပါတယ် (implicit) — manual order specify ဖို့ မလိုအပ်ပါ။ IAM role attach ပြီးမှ Lambda function run ခွင့်ရမယ့် scenario လိုမျိုးမှာ attribute reference မရှိပေမယ့် logical order လိုအပ်ရင် `depends_on = [aws_iam_role_policy_attachment.lambda_policy]` ကို ရေးပေးရပါတယ်.

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

hcl
resource "aws_subnet" "app" {
  vpc_id     = data.aws_vpc.existing.id
  cidr_block = "10.0.1.0/24"
}

# Implicit dependency: references aws_subnet.app.id above
resource "aws_instance" "web" {
  subnet_id     = aws_subnet.app.id
  instance_type = "t3.micro"
  ami           = "ami-0abcdef1234567890"
}

# Explicit dependency: no direct attribute reference, but order matters
resource "aws_instance" "worker" {
  ami           = "ami-0abcdef1234567890"
  instance_type = "t3.micro"
  depends_on    = [aws_instance.web]
}
You should see
$ terraform graph
# shows aws_subnet.app -> aws_instance.web -> aws_instance.worker
# as a dependency chain, in the order Terraform will create them

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

Resource ၂ ခု (တစ်ခုက တစ်ခုကို attribute reference လုပ်တဲ့) ရေးပြီး `terraform graph` command run ကြည့်ပါ — dependency order ကို output ထဲကနေ ဖတ်ကြည့်ပါ။

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

Dependency chain ရှည်လွန်းရင် (A depends on B depends on C...) apply time ကြာနိုင်ပါတယ် (Terraform က parallel create မလုပ်နိုင်တော့ပါ) — real dependency ရှိတဲ့အခါမှသာ reference/depends_on ထားပါ။

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

  • `depends_on` ကို လိုအပ်တိုင်း (implicit dependency ရှိနေတဲ့ resource ကိုပါ) ထပ်ရေးခြင်း — redundant ဖြစ်ပြီး code ရှုပ်ထွေးစေပါတယ်
  • Explicit dependency လိုအပ်တဲ့ scenario မှာ `depends_on` မရေးဘဲ resource ၂ ခု parallel create ဖြစ်သွားပြီး error တွေ့ရခြင်း

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

Resource ၂ ခု (တစ်ခုက တစ်ခုကို attribute reference လုပ်တဲ့) ရေးပြီး `terraform graph` command run ကြည့်ပါ — dependency order ကို output ထဲကနေ ဖတ်ကြည့်ပါ။

You'll know it worked when: $ terraform graph # shows aws_subnet.app -> aws_instance.web -> aws_instance.worker # as a dependency chain, in the order Terraform will create them

Dependencies & References | Thuta Learning