Terraform.tfvars versus variables.tf differences?

What is the difference between using terraform.tfvars versus variables.tf ? I am looking at https://learn.hashicorp.com/terraform/azure/variables_az but I am still not clear on the differences? And can I define variables in main.tf or should I just create a variable.tf at the main.tf directory level? Can we use terraform.tfvars at the same directory level as the main.tf and also have a second terraform.tfvars i another sub-directory which includes other *.tf to be consumed?

It seems like terraform.tfvars has a simplified syntax?
location = “westus”
prefix = “tf”

For variables.tf can the simple syntax used for terraform.tfvars also be used? or do we still need to use the below syntax. Also please explain '{}"? ,… does this mean that you will be prompted during terraform plan or terraform apply for the variable value?

variable “location” {}

variable “prefix” {
type = “string”
default = “my”

variable “tags” {
type = “map”
default = {
Environment = “Terraform GS”
Dept = “Engineering”

variable “sku” {
default = {
westus = “16.04-LTS”
eastus = “18.04-LTS”


The variable stanza:

variable "my_var" {

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 "my_var" {
  default = "foo"

Or 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_my_var=bar
  • Flags passed to the terraform command terraform apply -var 'my_var=bar'

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 file name
using the command line flag terraform apply -var-file=./nics.tfvars

Defining defaults in the variable stanza allow 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 override the defaults for development and test environments.

Regarding directories. a sub-directory in Terraform is a module. Terraform will only process *.tf files in the folder where you run terraform plan.

To include configuration in sub-folders you use the module syntax. For example, given I have a sub-folder network which contains my network configuration. I can import this into my config using the module stanza.


module "my_network" {
  source = "./network"