What is a tfvars file in Terraform and how do you use it?

A beginner-friendly guide to Terraform tfvars files. Learn how to define variables, use tfvars to customise deployments, and manage multiple environments.

What is a tfvars file in Terraform and how do you use it?
What is a tfvars file in Terraform and how do you use it?

When you are using Terraform, one of the key principles is that you can write reusable and scalable code that can be used in multiple environments without you having to rewrite or duplicate it.

And this is where the tfvars file comes in.  This file allows you to define and customise variable values quickly and efficiently making your deployments more consistent and much easier to manager. 

What is a tfvars file in Terraform?

A tfvars file in Terraform is used to define input variable values for a Terraform configuration. This file allows you to supply values for variables, making it easy to customise deployments for different environments (such as dev, test, or prod).

There are a number of ways you can customise variables within a Terraform deployment; however using a tfvars file is the most common and efficient. 

How to use tfvars files?

The best way to show you how to use a tfvars file is to walk through some example code.  Let’s set up a project to define an Azure resource group, 

# Define the Resource Group
resource "azurerm_resource_group" "resource_rg" {
  name 	= "rg-example"
  location = "uksouth"
  tags = {
	Environment = "Demo"
	Owner   	= "Sarah Lean"
  }

You can find the full code here

This base code works, however, we’ve hard coded information here, that will make it hard for us to use within other environments and we’ll need to duplicate code and ultimately waste a lot of engineer time changing those hard code values. 

So let’s create some variables, we want variables for:

  • The resource group name
  • Location
  • Tags

Within your project folder, create a file called variables.tf and we’ll define our variables in there.  Within each variable, we’ll include a description and a default value. 

variable "Resource_group_name" {
  description = "The name of the resource group"
  type        = string
  default     = "azurergtechielass"
}
variable "Location" {
  description = "The Azure region to deploy resources in"
  type        = string
  default     = "Sweden Central"
}
variable "Environment" {
    description = "The environment for the resources"
    type        = string
    default     = "Demo"  
}
variable "Owner" {
    description = "The owner of the resources"
    type        = string
    default     = "Techie Lass"  
}

Now that our variables are defined, we need to change the Terraform code to refer to the variables. 

# Define the Resource Group
resource "azurerm_resource_group" "resource_rg" {
  name     = var.Resource_group_name
  location = var.Location
  tags = {
    Environment = var.Environment
    Owner       = var.Owner
  }
}

Now if we were to deploy the Terraform code it would work fine and deploy an Azure resource group, using all the default values we defined in our variables.tf file. 

Let’s create a tfvars file

Instead of using the default values, let’s define our own using the terraform.tfvars file. 

Within your project folder, create the terraform.tfvars file and assign a value to each variable like this:

Resource_group_name = "techielass-demo-rg"
Location = "France Central"
Environment = "Blog"
Owner = "Sarah"

If we run a terraform plan we’ll see that Terraform is now pulling in the values from the tfvars file and not using any of the default values that we set initially. 

Terraform plan command with tfvars file
Terraform plan command with tfvars file

Using multiple tfvars files for different environments

The benefit of using tfvars files for your variables means you can separate tfvars files for each environment, for example: 

  • Dev.tfvars
  • Test.tfvars
  • Prod.tfvars

And then when you are running your deployment, you change your plan and apply commands to point to the correct tfvars file:

terraform plan -var-file="dev.tfvars"
terraform apply -var-file="dev.tfvars"

This means you don’t have to rewrite your code for each environment, and tweaking a tfvars file requires minimal time, meaning deployments can be faster. 

Summary

Using a tfvars file as part of your Terraform deployments makes your code more flexible, reusable and efficient.  By separating your variable values from your main configuration code you can avoid hardcoding values and make your code easier to maintain and scale across multiple environments.