Reading Time: 2 minutes
Share:
Twitter
LinkedIn
Facebook
Google+
Reddit
Whatsapp
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.

Shell

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.

Shell

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.

Note:

You will need to have your main.tf file

Shell

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.

Shell

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.

Share:
Twitter
LinkedIn
Facebook
Google+
Reddit
Whatsapp
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.

2 Comments

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.

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

*

I agree