Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between variables.tf and terraform.tfvars?

Tags:

terraform

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?

like image 630
aplusp Avatar asked May 02 '19 19:05

aplusp


People also ask

What are Terraform Tfvars?

A "terraform. tfvars" file is an alternative to using the "-var" flag or environment variables. The file defines the variable values used by the script. If the file is named "terraform. tvars" it is loaded by default.

How do you beat Terraform in Tfvars?

tfvars files created, you can then use the -var-file flag to pass in the name of the file for Terraform to pull in for passing Input Variables to the Terraform command. Multiple -var-file flags can be used on the same Terraform command to pass in multiple .

How do I create a variable TF file in Terraform?

Terraform variables allow you to write configuration that is flexible and easier to re-use. Add a variable to define the instance name. Create a new file called variables.tf with a block defining a new instance_name variable. Note: Terraform loads all files in the current directory ending in .

How do you use Terraform Vars?

Terraform variables can be defined within the infrastructure plan but are recommended to be stored in their own variables file. All files in your Terraform directory using the . tf file format will be automatically loaded during operations. Create a variables file, for example, variables.tf and open the file for edit.


Video Answer


3 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 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.

like image 137
Adil B Avatar answered Oct 23 '22 21:10

Adil B


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.

like image 34
Berimbolinho Avatar answered Oct 23 '22 21:10

Berimbolinho


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.

like image 21
Varun Avatar answered Oct 23 '22 20:10

Varun