TF basic course - lesson "Using Variables in Terraform"

Hello, I am not clear about the variable definition precedence.

In the lesson, the order is described from 1 to 4, but at the end it says that option 4 (passing the variable via cmd line) takes the priority over the rest. So why is that marked as 4th in line and not the 1st?
What is the correct order for tf to take the definition?
And why are there the big red X close to the first three options?

The screenshot is from minute 4:40

Thanks in advance.

Hi @Mundofik

Terraform checks variable values in a specific order, and the last one wins if there are conflicts. Here’s the deal, short and clear:

  1. Environment variables
  2. terraform.tfvars
  3. *.auto.tfvars files
  4. Command-line flags (-var) – this one takes priority over everything

The red X’s in the image just show that those values got overridden by the one passed with -var. The green check means that’s the value Terraform actually used.

Thank you Raymond.
What if I have the variable defined with the first 3 points, i.e. TF_VARS, .tfvars, *.auto.tfvars.
Which one is taken?

The value from the .auto.tfvars file will be used.

Ok, so do I get it right that the evaluation is done from top to bottom, but the value taken is done bottom up?

Yes, that’s a good way to think about it.

Perfect, thank you very much!

1 Like