Difference between variables.tf and terraform.tfvars?

Shanmuganathan Raju
2 min readMay 14, 2022

These two files are similar but are not the same. A variables.tf file is used to define variables, it’s type and set a default value. A terraform.tfvars file is used to set the actual values of the variables. You can also set default values for all your variables and not use tfvars files(It is not a recommended way).

The difference between them is that of declaration and assignment. It is not compulsory for the variable block to be present in variables.tf file. It can be present in any tf file. The function of the variable block is to let Terraform be aware of the existence of this variable. There are multiple ways of assigning a value to a declared variable :

  • Pass values as key-value pairs by using the -var option on the terraform plan or terraform apply command line.
  • Use -var-file option to pass one or more .tfvars fils to set values for many variables at once.
  • Create a terraform.tfvars file, or files named .auto.tfvars. These files are loaded automatically

There is also the option declaring a variable with a default value. Default values are suitable for situations where there is a good default behavior, say the name of the app, which is remain constant across environments.

However, for most cases the value will not be same throughout. These different methods of assigning variables deals with these differences. It allows to deploy different copies of the same infrastructure.

Examples UseCase:

Let’s declare a variable with the name “instance_name” and with default value.

The variable details:

variable "instance_name" {
}

Is used to define a variable for your Terraform configuration. If you run terraform plan or apply with a variable defined but no default. Terraform will prompt you for the value.

It can have a default value in the stanza block:

variable "instance_name" {
default = "instances-ne"
}

There are three ways to set or override defaults, or in the absence of a default, set the value. These are:

  • terraform.tfvars files
  • Environment variables prefixed with TF_VAR, e.g TF_VAR_instance_name=bar
  • Flags passed to the terraform command terraform apply -var 'instance_name=developmentinstance-ne'

terraform.tfvars is a default file name, if this is present in your root directory then terraform will load this file and apply the values. You can also specify a custom filename
using the command line flag terraform apply -var-file=./dev.tfvars

Defining defaults in the variable allows you to set sane defaults for your configuration. However, you might want to override things. For example, you might have a dev.tfvars, test.tfvars which overrides the defaults for development and test environments.

Official Link for Terraform Variables

--

--

Shanmuganathan Raju

A Multicloud Architect, with more than 16 years in the information technology industry with experiences in architecting, solution designing and Cloud Migration.