27

What is the difference between variables.tf and terraform.tfvars? Both can define (assign values) to variables right? Is there any difference in scope or behavior of variables defined in these two? As far as assigning values to variables, can one be used in place of the other?

1
  • Please add some details: What language (or lib?) are you talking about? At least add some tags to give your question a context.
    – Jere
    May 2, 2019 at 21:40

5 Answers 5

29
  • variables.tf - here, you define the variables that must have values in order for your Terraform code to validate and run. You can also define default values for your variables in this file. Note that you don't need to define all of your variables in a file named variables.tf - they can be defined anywhere, but this practice is encouraged for organizational purposes.

  • terraform.tfvars - this file contains one or more variablename=variablevalue pairs. When Terraform loads this file, it looks for any variables in your Terraform with the name variablename and sets their value to be variablevalue. You can't define new variables here, and can only set the values of existing ones defined in variables.tf.

0
3

In variables.tf you create and potentially assign values. A normal use case is that you have a module where you need to assign name in the resource. You can then create a variable called name as well. So in your definition you do the assignment:

name = "${var.name}"

When you run terraform plan or terraform apply all your variables need to be set. You can set them in the actual code, like name = "test", enter them into the console when prompted upon execution or pass them into the command by running terraform apply -var-file terraform.tfvars.

tfvars files contain assignments and can be used when you have multiple environments. By passing this file into the command you will assign the values to the corresponding variables. For example you can use it to assign region, account-ids etc. If these files contain sensitive credentials make sure to .gitignore it and store it in a separate credentials handler.

2

The variables.tf file is used to define variables for your terraform configuration. The variables are defined as below

variable "region" {
    description = "Region of AWS VPC"
}

or

variable "region" {
    default = "us-east-1"
    description = "Region of AWS VPC"
}

If the default value for the variable is not defined and you run terraform plan or apply, terraform will prompt you for the value.

There are three ways to set the value to the variables defined in variablea.tf file. These three ways can also be used to override the default value of the variables.

  1. terraform.tfvars files
  2. Environment variables prefixed with TF_VAR, e.g. TF_VAR_my_var=bar
  3. Flags passed to the terraform command terraform apply -var 'my_var=bar'

terraform.tfvars file can have the following value set for the variable region that is defined in varaible.tf

region = "us-east-1"

In short variables.tf is used to define variables and terraform.tfvars is used to provide or set values to the variables defined in varaibles.tf file.

0

variables.tf: declare variables, optionally including

  • type
  • validation rules
  • default value
  • ...

https://www.terraform.io/language/values/variables#input-variable-documentation

terraform.tfvars: define variables by assigning a value to them https://www.terraform.io/language/values/variables#variable-definitions-tfvars-files

0

Simple Understanding

Variables.tf --> To create variables
terraform.tfvars --> To assign values to those variables

variables.tf

variable "region" {
   description = "Region of AWS"
}

terraform.tfvars

region="us-west-1"

So, when you run terraform apply, it will load values from tfvars fie for the variables created. If there is no tfvars file, it will take the default value which you will assign at the time of declaring itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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