BM: Baca dalam Bahasa Melayu
World T2: Inputs & Outputs
tfvars files
You will learn: to keep variable values in files, and which value wins.
Checked with OpenTofu 1.13 on 1 Nov 2026
The problem
You type -var shop=โฆ every time you build. One day you will forget it. Where can the values live?
Pick one answer.
Watch
terraform.tfvars already holds the shop name. Type tofu plan. No question this time.
Blueprint (your .tf files)init done
local_file.signnot built yet
Built (the state file)
Nothing built yet.
Change a file, then run tofu plan to see what it means.
A .tfvars file: A file of variable values. terraform.tfvars and *.auto.tfvars load by themselves; others need -var-file. holds values. terraform.tfvars loads by itself.
Predict
penang.tfvars holds the Penang shop. You type tofu plan -var-file=penang.tfvars. Which shop name wins?
shop = "Kedai Kancil Penang"
tables = 8Pick one answer.
Try it
Build the Penang sign: tofu apply -var-file=penang.tfvars, then yes.
Blueprint (your .tf files)init done
local_file.signnot built yet
Built (the state file)
Nothing built yet.
Change a file, then run tofu plan to see what it means.
Hint
tofu apply -var-file=penang.tfvars, then yes.Check
Who wins? Put the weakest at the top. Later ones beat earlier ones.
Weakest at the top. Put these in order. Use the arrows, or drag.
- -var or -var-file on the command line
- files ending in .auto.tfvars
- TF_VAR_shop, an environment variable
- terraform.tfvars
- default in variables.tf
Check
Safe to commit to Git?
Swipe right for good, left for needs work. Or use the buttons.
penang.tfvars with the shop name and 8 tables
You did it!
You can now keep values in .tfvars files and know which one wins.
Go deeper (optional)
Any file ending in .auto.tfvars loads by itself too, in name order. export TF_VAR_tables=6 gives var.tables a value from your shell. In a pipeline, -input=false stops OpenTofu from asking. A missing value then fails at once, instead of waiting forever.
Bahasa Melayu
Anda taip -var shop=โฆ setiap kali membina. Satu hari anda akan terlupa. Di mana nilai itu boleh disimpan?
terraform.tfvars sudah simpan nama kedai. Taip tofu plan. Kali ini tiada soalan.
Fail .tfvars simpan nilai. terraform.tfvars dibaca sendiri.
penang.tfvars simpan kedai Penang. Anda taip tofu plan -var-file=penang.tfvars. Nama kedai mana yang menang?
shop = "Kedai Kancil Penang"
tables = 8
Bina papan tanda Penang: tofu apply -var-file=penang.tfvars, kemudian yes.
tofu apply -var-file=penang.tfvars, kemudian yes.Siapa menang? Letak yang paling lemah di atas. Yang kemudian menang atas yang awal.
Selamat untuk commit ke Git?
Kini anda boleh simpan nilai dalam fail .tfvars dan tahu yang mana menang.
You finished this before. Practice is always welcome!
Official docs: OpenTofu: input variables ยท OpenTofu: variable definition precedence
You've learned for 25 minutes. A short break helps your brain.
Sprint done! Great focus. Stretch for a minute?