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

Published: (December 16, 2025 at 03:01 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

Sarah Lean 🏴󠁧󠁢

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

What is a tfvars file in Terraform?

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

There are many ways to customise variables within a Terraform deployment; however, using a tfvars file is the most common and efficient method.

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.

1. Define the resource group (hard‑coded)

# 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, but the hard‑coded values make it difficult to reuse across environments. To avoid duplication, we’ll introduce variables.

2. Create variables.tf

Create a file called variables.tf in your project folder and define the variables there. Include a description and a default value for each variable.

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"
}

3. Reference the variables in the resource

# 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
  }
}

If you run terraform apply now, the resource group will be created using the default values from variables.tf.

4. Create a terraform.tfvars file

Instead of using the defaults, you can override them with a tfvars file. Create terraform.tfvars in the project folder and assign values to each variable:

resource_group_name = "techielass-demo-rg"
location            = "France Central"
environment         = "Blog"
owner               = "Sarah"

Running terraform plan now shows that Terraform is pulling in the values from the tfvars file rather than the defaults.

Terraform plan command with tfvars file

Terraform plan command with tfvars file

Using multiple tfvars files for different environments

The benefit of tfvars files is that you can maintain separate files for each environment, for example:

  • dev.tfvars
  • test.tfvars
  • prod.tfvars

When running Terraform, point to the appropriate file:

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

This approach eliminates the need to rewrite code for each environment; you only modify the relevant tfvars file, making deployments faster and less error‑prone.

Terraform tip

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 hard‑coding values and make your code easier to maintain and scale across multiple environments.

Back to Blog

Related posts

Read more »

Day 7 – Type Constraints in Terraform

On day 5 we saw variables in action. Today we dive deeper into type constraints in Terraform. Primitive Types Primitive types are the basic building blocks: str...