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