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

Data Sources

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

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

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

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

Resource block က 'အသစ်ဖန်တီးမယ်' ဆိုလိုပေမယ့် Data Source block (`data "<type>" "<name>" { ... }`) ကတော့ 'ရှိပြီးသား resource ရဲ့ information ကို ဖတ်ရုံပဲ' ဆိုလိုတာပါ — create/update/destroy ဘာမှ မလုပ်ပါ, query ရုံပါ။ Team တခြားက ကြိုတင် create ထားတဲ့ shared resource (VPC, security group) ကို ကိုယ့် config ထဲမှာ 'ထပ်ဖန်တီးစရာမလို', reference ပဲ ယူချင်တဲ့အခါ Data Source ကို သုံးပါတယ်.

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

`data "aws_vpc" "existing" { tags = { Name = "production-vpc" } }` လို့ ရေးထားရင် Terraform က AWS ကို query ပို့ပြီး 'production-vpc' tag ပါတဲ့ VPC ကို ရှာပြီး, ရလာတဲ့ VPC ID ကို `data.aws_vpc.existing.id` နဲ့ ကိုယ့် resource ထဲမှာ reference လုပ်နိုင်ပါတယ် — VPC ကို ကိုယ်တိုင် create ပြန်လုပ်စရာ မလိုပါဘူး.

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

hcl
data "aws_vpc" "existing" {
  tags = {
    Name = "production-vpc"
  }
}

resource "aws_subnet" "app" {
  vpc_id     = data.aws_vpc.existing.id
  cidr_block = "10.0.1.0/24"
}
You should see
$ terraform plan
data.aws_vpc.existing: Reading...
data.aws_vpc.existing: Read complete after 1s

  + resource "aws_subnet" "app" {
      + vpc_id = "vpc-0123456789abcdef0"
    }

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

Data source block တစ်ခု (ကိုယ်ပိုင် provider အလိုက်) ရေးကြည့်ပြီး, `terraform plan` run ကာ 'Reading...' step ကို ရှင်းရှင်းလင်းလင်း ကြည့်ကြည့်ပါ။

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

Data source ရဲ့ query result က apply လုပ်တဲ့ အချိန်ကို depend ပါတယ် — target resource က query လုပ်တဲ့ ချိန်မတိုင်ခင် ဖျက်/ပြောင်းသွားရင် plan/apply fail ဖြစ်နိုင်ပါတယ်။

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

  • Data source ကို resource လို့ ထင်ပြီး create/destroy ဖြစ်မယ်လို့ မျှော်လင့်ခြင်း — data source က read-only ပါ
  • Data source ရဲ့ filter (tags, name) ကို ရှုပ်ထွေးအောင် ရေးပြီး result တစ်ခုထက်ပိုပြီး ကိုက်ညီသွားခြင်း (error ဖြစ်နိုင်ပါတယ်)

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

Data source block တစ်ခု (ကိုယ်ပိုင် provider အလိုက်) ရေးကြည့်ပြီး, `terraform plan` run ကာ 'Reading...' step ကို ရှင်းရှင်းလင်းလင်း ကြည့်ကြည့်ပါ။

You'll know it worked when: $ terraform plan data.aws_vpc.existing: Reading... data.aws_vpc.existing: Read complete after 1s + resource "aws_subnet" "app" { + vpc_id = "vpc-0123456789abcdef0" }

Data Sources | Thuta Learning