Reading Time: 2 minutes
Follow by Email

As I use Terraform more my love for it grows. In this article I am going to show you how to store the state of your environment to a tfstate file that is saved in Azure Storage. This file is in the JSON format and is used by Terraform to make sure it only applies the difference every time you run it. If you would like to read more about tfstate files you can read the documentation here.

So enough talk lets get to it.

Configure the Storage Account

First, we need to create and configure the storage account. Bellow is a simple script that will do this for us. Just make sure you have logged in to Azure first using the az login command.


Copy the output at the end to something like the below, we will need this information later.

resource_group_name: Terraform-States
storage_account_name: terraformstate17297
container_name: terraformstate-tst
access_key: wNuyu6l2h1IHc5iuBgvWQnz/WkXhFcNhGitVkIqZWNMkMtjMLPJFC9VE6hDj+u0c1Lk8TUtM/rI+dSEZpdZCJw==

Configure the Terraform state backend

Now that we have created an Azure storage account and Container for our terraform states we need to create a file called backend.tfvars to store the backend config in.

In visual studio code copy the bellow.


Now under resource_group_name enter the name from the script. Do the same for storage_account_name, container_name and access_key.

For the Key value this will be the name of the terraform state file. For this example I am going to use tst.tfstate

Your backend.tfvars file will now look something like this.

In your Windows subsystem for Linux window or a bash prompt from within VS Code navigate to your terraform project folder that has the backend.tfvars file in and initiate Terraform using the following.


You will need to have your file


You can create multiple backend.tfvar files for each of your environments. So one for Test, Dev, and Prod.

If you are happy with them being in the same storage account and container you can just copy the backend.tfvar file and then under the Key value add test/, dev/, or prod/ before the file name. It would look like.


This will actually create you a folder under your Azure Storage container.

Then save it as a different file, such as prodbackend.tfvar. Then when you need to initialise Terraform for the different environments just change the file name to which one you are working with.

If you would like a separate storage container, storage account , etc then just use the above script and change the variables.

Thanks for reading and I hope you found this article helpful. If you have any questions reach out.

Follow by Email

Pixel Robots.

I’m Richard Hooper aka Pixel Robots. I started this blog in 2016 for a couple reasons. The first reason was basically just a place for me to store my step by step guides, troubleshooting guides and just plain ideas about being a sysadmin. The second reason was to share what I have learned and found out with other people like me. Hopefully, you can find something useful on the site.


reddy · February 26, 2019 at 10:28 am

Hi, the article is nice, I am having a question, I have stored my state file in the container. How can I share my state file to the other people on the team?

    Pixel Robots. · February 26, 2019 at 10:55 am

    If they use the backend.tfvars file they will be able to access the file too.

Jonathan Reynes · May 25, 2019 at 5:03 am

I get this when following your steps and I am on version 0.12.0
Am I doing something wrong?
terraform init -backend-config=”path=/backend.tfvars”

Error loading backend config: 1 error(s) occurred:
Initializing the backend…

* terraform.backend: configuration cannot contain interpolations

The backend configuration is loaded by Terraform extremely early, before
the core of Terraform can be initialized. This is necessary because the backend
dictates the behavior of that core. The core is what handles interpolation
processing. Because of this, interpolations cannot be used in backend

If you’d like to parameterize backend configuration, we recommend using
partial configuration with the “-backend-config” flag to “terraform init”.

I know currently interpolation for terraform {backend “azurerm” {}} resources are not supported. Meaning, you simply cannot place a var.whatever variable inside backend. I am not doing this, but am doing exactly as the tutorial shows. Am I missing something? I have
resource “azurerm_resource_group” “state-demo-secure” {
name = “state-demo”
location = “eastus”


provider “azurerm” {
version = “1.29.0”
subscription_id = “${var.azurerm_provider[“subscription_id”]}”
client_id = “${var.azurerm_provider[“client_id”]}”
client_secret = “${var.azurerm_provider[“client_secret”]}”
tenant_id = “${var.azurerm_provider[“tenant_id”]}”

with an associated providers.tfvars

    Pixel Robots. · May 27, 2019 at 4:06 pm

    Sorry for the delay in getting back to you. Are you able to show your backend.tfvars file also?

    Jonathan Reynes · May 28, 2019 at 7:11 pm

    Please ignore ^
    The error occurred because I was assigning one of the backend.tfvars variables incorrectly with “:” instead of “=”

      Pixel Robots. · May 28, 2019 at 7:20 pm

      Glad you got it sorted.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *