How To Use and Run Terraform TFVARS

Terraform is a tool used to define and deploy infrastructure as code. It is a popular choice for managing infrastructure as code because it is easy to use, has a large and active community, and is supported by a number of cloud providers.

TFVARS is short for Terraform variables. Variables are a way to pass data into a Terraform configuration and can be used to parameterize the configuration. They can be defined in a file with a .tfvars extension or can be passed in as command-line arguments when running Terraform.

TFVARS files are used to store the values for variables. They are in the same format as Terraform configuration files, with variable names and their corresponding values defined as key-value pairs. For example, I have a TFVARS file called vars.tfvars

vm_user = "myadmin"
principal_id = "20994565-01"

You can then reference these variables in your Terraform configuration using as shown in the example below:

resource "azurerm_role_assignment" "assign-vm-role" {
  scope                =
  role_definition_name = "Virtual Machine Administrator Login"
  principal_id         = var.principal_id


How To Run a TFVARS File

To run a Terraform configuration that references a TFVARS file, you will need to provide the path to the file as an argument to the terraform apply command as shown below.

terraform apply -var-file=path/to/vars.tfvars

Success! You're on the list.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.