53

I've been researching this but can't find the distinction. A variables.tf file can store variable defaults/values, like a terraform.tfvars file.

What's the difference between these two and the need for one over the other? My understanding is if you pass in the var file as an argument in terraform via the command line.

There is a thread about this already and the only benefit seems to be passing in the tfvars file as an argument, as you can "potentially" do assignment of variables in a variable.tf file.

Is this the correct thinking?

2
  • 1
    The answers on this are way better than the "original" question, fwiw
    – d8aninja
    Oct 18, 2021 at 17:39
  • youtube.com/…
    – GurbaniX
    Oct 27, 2022 at 15:01

2 Answers 2

74

The distinction between these is of declaration vs. assignment.

variable blocks (which can actually appear in any .tf file, but are in variables.tf by convention) declare that a variable exists:

variable "example" {}

This tells Terraform that this module accepts an input variable called example. Stating this makes it valid to use var.example elsewhere in the module to access the value of the variable.

There are several different ways to assign a value to this input variable:

  • Include -var options on the terraform plan or terraform apply command line.
  • Include -var-file options to select one or more .tfvars files to set values for many variables at once.
  • Create a terraform.tfvars file, or files named .auto.tfvars, which are treated the same as -var-file arguments but are loaded automatically.
  • For a child module, include an expression to assign to the variable inside the calling module block.

A variable can optionally be declared with a default value, which makes it optional. Variable defaults are used for situations where there's a good default behavior that would work well for most uses of the module/configuration, while still allowing that behavior to be overridden in exceptional cases.

The various means for assigning variable values are for dealing with differences. What that means will depend on exactly how you are using Terraform, but for example if you are using the same configuration multiple times to deploy different "copies" of the same infrastructure (environments, etc) then you might choose to have a different .tfvars file for each of these copies.

Because terraform.tfvars and .auto.tfvars are automatically loaded without any additional options, they behave similarly to defaults, but the intent of these is different. When running Terraform in automation, some users have their automation generate a terraform.tfvars file or .auto.tfvars just before running Terraform in order to pass in values the automation knows, such as what environment the automation is running for, etc.

The difference between the automatically-loaded .tfvars files and variable defaults is more clear when dealing with child modules. .tfvars files (and -var, -var-file options) only apply to the root module variables, while variable defaults apply when that module is used as a child module too, meaning that variables with defaults can be omitted in module blocks.

1
  • 1
    This is a very good explanation.
    – look
    May 26, 2022 at 5:29
62

A variables.tf file is used to define the variables type and optionally set a default value.

A terraform.tfvars file is used to set the actual values of the variables.

You could set default values for all your variables and not use tfvars files at all.

Actually the objective of splitting between the definitions and the values, is to allow the definition of a common infrastructure design, and then apply specific values per environment.

Using multiple tfvars files that you give as an argument allows you to set different values per environment : secrets, VM size, number of instances, etc.

3
  • What is the difference between var.tf and variables.tf ? Oct 8, 2020 at 7:05
  • 1
    @JananathBanuka same Dec 23, 2020 at 12:32
  • 7
    One additional point worth noting is that the terraform.tfvars file cannot declare new variables on its own. It can only provide values to the variables declared in the corresponding variables.tf file. Feb 16, 2022 at 13:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.