World map

 

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.

Blueprint: 1 resources. Built: 0.

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 = 8

Pick 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.

Blueprint: 1 resources. Built: 0.

Change a file, then run tofu plan to see what it means.

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.

1 / 3

penang.tfvars with the shop name and 8 tables

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.