Part 8: Terraform with Azure – Deploy terraform.tfvars file

Reading Time: 5 minutes

My journey learning Terraform to allow me to deploy workloads into Microsoft Azure continues. If you missed the previous posts, please visit the links below.

Part 1: Terraform with Azure – How to install Terraform
Part 2: Terraform with Azure – How to install Azure cli
Part 3: Terraform with Azure – How to Install Visual Studio Code
Part 4: Terraform with Azure – How to install Azure Terraform Plugin in Visual Studio Code
Part 5: Terraform with Azure – Install Git and initialise repository
Part 6: Terraform with Azure – Deploy resources in Azure
Part 7: Terraform with Azure – Deploy a variables file in Terraform

Did you know:
A sub directory in Terraform is known as a module. Terraform will only process the *.tf files located in the folder where you run the command terraform plan. If you would like to include configuration from sub directories, then you use the module syntax. For example, If I have a sub directory named networking, which contains my azure network configuration, I can import the networking directory into my working config directory by specifying the below command. We’ll cover more on this as we move on.

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

In this blog post I go through the process of compiling a .tfvars file, but before I continue,

What is a .tfvars file?

This is a confusing topic as terraform.tfvars and serve a similar role, as in both are variable files. However, a tfvars file stores the default values from a file and allows you to override values if required. Still a little confusing, right? Let’s try to work this out by discussing why we would use a tfvars files.

You could create tfvars files to deploy into multiple environments such as prod.tfvars and dev.tfvars. Therefore, the .tf files would hold the default settings that are common among all configurations but the tfvars files could hold different values. Another example of when you could make use of a tfvars file is for applications which have their own unique settings.

Something else to note, Terraform will automatically load the variables from a tfvars file if it’s named terraform.tfvars or *.auto.tfvars and placed in the same directory as your Terraform configuration file. If you wish to execute different named tfvars files, such as, prod.tfvars, staging.tfvars, test.tfvars etc, you can specify a custom tfvars file using the command line parameter -var-file and then specify the path to a variable file. I have specified examples below,

terraform plan -var-file="prod.tfvars"


terraform plan -var-file="app1.tfvars"

So custom named tfvars files can be called upon based on the platform you are deploying to.

Let’s get started from where we left in Part 7. We removed the default values from the file, this action resulted in terraform prompting for values when we executed the command terraform plan.

  1. Create a new file named terraform.tfvars

  2. I have loaded both and the newly created terraform.tfvars file in a split screen as seen below.

3. In the previous blog post, I removed the default values. For the purpose of this demo, I have hashed the default values as per the screen shot above. If you removed the default values from the file, your file will look similar to the one below.

variable "resource_group_name" {
   type = string
  description = "Azure Resource Group Name"

variable "resource_group_location" {
   type = string
  description = "Azure Resource Group location"

4. I will now copy the text resource_group_name and resource_group_location to the newly created terraform.tfvars file.

5. Next, I input the values for the variables I have copied to the terraform.tfvars file.

My terraform.tfvars file code below,

resource_group_name = "RG-DEMO-PROD1"
resource_group_location = "UK South"

6. Save changes. Next we run terraform plan. Terraform should no longer prompt us to input default values, instead Terraform will automatically recognise the terraform.tfvars file and apply the values. We named the file terraform.tfvars, therefore, the file will be loaded automatically.

If there was a requirement to run a different named tfvars file such as prod.tfvars, you would specify a command after terraform -plan


terraform plan -var-file="prod.tfvars"

Type terraform plan -h for a list of available commands

Back to deploying our resource group using the terraform.tfvars file, let’s give it a go. The results are as expected. Terraform has recognised the terraform.tfvars file and not prompted us to insert values manually.

A terraform.tfstate.backup file is created automatically in case the state file is lost or corrupted and simplifies recovery if needed.

7. So we executed terraform plan and the output shows that our tfvars file was successfully picked up and the values processed by Terraform. Next, we run terraform apply. Review and input ‘yes’

Let’s visit the Azure Portal. The resource group was created successfully.

8. Next, I would like to override a value within the tfvars file, for example, I want to create a resource group but with a name different to the one specified in the terraform.tfvars file. As per the below, my resource group is RG-DEMO-PROD1 and variable name is resource_group_name.

resource_group_name = "RG-DEMO-PROD1"
resource_group_location = "UK South"

9. I type the command below to override the tfvars file for variable resource_group_name. I specify the resource group NEW-RG.

terraform plan -var "resource_group_name=NEW-RG"

10. The result, Terraform reports that the resource group I created earlier will be replaced with the new resource group specified in the command I executed. The old group will be destroyed. I won’t be applying the command below as I only wanted to try out the variable over ride command.

Access the next blog post for part 9, where I deploy a VNET, Subnet, NSG and a tag in Azure. Part 9: Terraform with Azure – Deploy a VNET and Subnet

Thanks for following