> ## Content Index
> Fetch the complete content index at: https://www.techielass.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# What is a tfvars file in Terraform and how do you use it?
- URL: https://www.techielass.com/what-is-a-tfvars-file-in-terraform-and-how-do-you-use-it/
- Published: 2025-11-25T08:01:01.000Z
- Updated: 2026-01-13T08:41:39.000Z
- Description: A beginner-friendly guide to Terraform tfvars files. Learn how to define variables, use tfvars to customise deployments, and manage multiple environments.
- Author: Sarah Lean
- Tags: Terraform, Azure

When you are using [Terraform](https://developer.hashicorp.com/terraform?ref=techielass.com), 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](https://www.techielass.com/tag/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](https://www.techielass.com/tag/azure) resource group, 

```terraform
# 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](https://github.com/weeyin83/terraform-tfvars-example?ref=techielass.com). 

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](http://variables.tf/?ref=techielass.com) and we’ll define our variables in there. Within each variable, we’ll include a description and a default value. 

```terraform
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. 

```terraform
# 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](http://variables.tf/?ref=techielass.com) 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:

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

```

If we run a terraform plan we’ll see that [Terraform](https://www.techielass.com/introduction-to-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](https://storage.ghost.io/c/08/96/08960c71-63a2-449b-91b1-8d4628166dd2/content/images/2025/10/image-14.png)

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:

```bash
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. 

[![](https://storage.ghost.io/c/08/96/08960c71-63a2-449b-91b1-8d4628166dd2/content/images/2025/10/blogcoffee-2-3-7.png)](https://www.buymeacoffee.com/techielass?ref=techielass.com)

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