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?
-
Please add some details: What language (or lib?) are you talking about? At least add some tags to give your question a context.– JereMay 2, 2019 at 21:40
5 Answers
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 namedvariables.tf
- they can be defined anywhere, but this practice is encouraged for organizational purposes.terraform.tfvars
- this file contains one or morevariablename
=variablevalue
pairs. When Terraform loads this file, it looks for any variables in your Terraform with the namevariablename
and sets their value to bevariablevalue
. You can't define new variables here, and can only set the values of existing ones defined invariables.tf
.
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.
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.
- terraform.tfvars files
- Environment variables prefixed with TF_VAR, e.g. TF_VAR_my_var=bar
- 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.
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
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.