BM: Baca dalam Bahasa Melayu
World T2: Inputs & Outputs
Data sources: read what exists
You will learn: to read something that already exists with a data source, without owning it.
Checked with OpenTofu 1.13 on 1 Nov 2026
The problem
The chef writes todayβs recipe in recipe.txt by hand. Your blueprint needs its words for the menu. Should OpenTofu build recipe.txt?
Pick one answer.
Watch
A data source: A data block that reads something that already exists. OpenTofu never changes or destroys it. reads something that already exists. Resource or data?
Drag a card into a box. Or tap the card, then tap the box.
Try it
The menu copies the recipe. Type tofu apply, then yes. Then cat menu.txt.
Blueprint (your .tf files)init done
local_file.menunot built yet
Built (the state file)
Nothing built yet.
Change a file, then run tofu plan to see what it means.
data "local_file" "recipe" {
filename = "recipe.txt"
}Predict
The chef changes recipe.txt to Roti canai. You run a plan. What happens to menu.txt?
Pick one answer.
Try it
Be the chef: echo "Roti canai with dhal" > recipe.txt. Then tofu apply, and yes.
Blueprint (your .tf files)init done
local_file.menu
Built (the state file)state #1
local_file.menu
- recipe
- Nasi lemak with sambal
Change a file, then run tofu plan to see what it means.
Hint
First theecho line, then tofu apply, then yes.Check
The chef deleted recipe.txt. Plan stops. Tap the line that says why.
Tap the lines that match.
0 of 1 found
You did it!
You can now read existing things with a data source.
Go deeper (optional)
In the cloud, data sources look up things like an existing network. The plan shows them as Reading.... A resource that must wait for another can add depends_on. Usually a reference is enough.
Bahasa Melayu
Tukang masak tulis resepi hari ini dalam recipe.txt dengan tangan. Pelan anda perlukan perkataannya untuk menu. Patutkah OpenTofu bina recipe.txt?
Data source (sumber data) baca sesuatu yang sudah wujud. Resource atau data?
Menu salin resepi itu. Taip tofu apply, kemudian yes. Kemudian cat menu.txt.
data "local_file" "recipe" {
filename = "recipe.txt"
}
Tukang masak tukar recipe.txt jadi Roti canai. Anda jalankan plan. Apa jadi pada menu.txt?
Jadi tukang masak: echo "Roti canai with dhal" > recipe.txt. Kemudian tofu apply, dan yes.
echo, kemudian tofu apply, kemudian yes.Tukang masak padam recipe.txt. Plan berhenti. Tekan baris yang beri sebabnya.
Kini anda boleh baca benda yang sudah wujud dengan data source.
You finished this before. Practice is always welcome!
Official docs: OpenTofu: data sources
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?