Remote Terraform State file (terraform.tfstate) in Azure Blob Storage

Terraform allows us to perform IaC deployments. We can apply and destroy infrastructure quickly and automatically. To perform these operations, one of Terraform's fundamental files is the <terraform.tfstate> file. What is State in Terraform

AI-assisted translation, approved by Mateo.

Terraform allows us to perform IaC deployments. We can apply and destroy infrastructure quickly and automatically. To perform these operations, one of Terraform's fundamental files is the <terraform.tfstate> file.

What is State in Terraform and what is it used for?

Terraform needs to store the state, or "state", of the infrastructure and its configuration. This "real-world" information is stored in the file <terraform.tfstate>. By storing the state of resources and their dependencies, Terraform can determine what changes it needs to apply to the existing infrastructure to achieve the desired state defined in the code: the "desired state".

This file stores information about the resources that Terraform has created or configured, and it remains updated as changes are made to the infrastructure under the tool's radar.

It is crucial to handle it carefully, as it may contain sensitive information. It is also essential to ensure that the state does not run into conflicts, especially in large teams, by using locks or "locking".

By default, the state is stored in the "local" backend: a file is created in the path where our ".tf" files are located. However, in team environments or to ensure better security and versioning, it is advisable to use remote backends such as Azure Blob Storage, AWS S3, among others.

Remote State

Remote State is the recommended solution for collaborative work with Terraform. It will allow several people/teams to work on the same infrastructure. Through a fully functional state backend, Terraform can use remote State Lock as a measure to prevent two or more users from running Terraform simultaneously, thus ensuring that each run begins with the most recently updated state.

By using Azure Blob Storage (or any remote storage solution), you ensure that your Terraform state is centralized, encrypted, versioned, and accessible to those who need it, which is essential for properly managing infrastructure with Terraform, especially in team and production environments.

Now, let's see how to configure it in our Azure subscription

Connect to Azure CLI

We must log in to our Azure tenant and subscription. To do this, we must enter the following commands in Azure CLI:

  • az login
  • az account list –output table
  • az account set –subscription <SubscriptionId>

Create the 'tfstate' Resource Group, Storage Account, and Container:

To create the 'tfstate' Resource Group, Storage Account, and Container, we must run the following script in PowerShell:

PowerShell
$RESOURCE_GROUP_NAME='tfstate'
$STORAGE_ACCOUNT_NAME="tfstate$(Get-Random)"
$CONTAINER_NAME='tfstate'
# Create resource group
az group create --name $RESOURCE_GROUP_NAME --location eastus
# Create storage account
az storage account create --resource-group $RESOURCE_GROUP_NAME --name $STORAGE_ACCOUNT_NAME --sku Standard_LRS --encryption-services blob
# Create blob container
az storage container create --name $CONTAINER_NAME --account-name $STORAGE_ACCOUNT_NAME

Save the Access Key as an environment variable

To access the file, Terraform must have the Storage account Access Key. We must store the key value in an environment variable called <ARM_ACCESS_KEY>. To do this, we must run the following script:

PowerShell
$ACCOUNT_KEY= az storage account keys list --resource-group $RESOURCE_GROUP_NAME --account-name $STORAGE_ACCOUNT_NAME --query '[0].value' -o tsv
env:ARM_ACCESS_KEY=$ACCOUNT_KEY

Create our main.tf file

Now that we have our Storage Account ready and Terraform has the Access Key as an environment variable, we can create our first Terraform file. In this file, we will configure our "AzureRM" provider and take the opportunity to configure the backend configuration block.

HCL
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>3.0"
}
}
backend "azurerm" {
resource_group_name = "tfstate"
storage_account_name = "<storage_account_name>"
container_name = "tfstate"
key = "terraform.tfstate"
}
}
provider "azurerm" {
features {}
}

We must replace <storage_account_name> with the name of our Storage Account.

In this case, it will be tfstate1953629526.

Initialize configuration: <Terraform Init>

We will initialize the Terraform configuration with the <terraform init> command

Check file creation

We can confirm that the file has been created in our Blob Container.

As we can see, the file was created in our container. It should also be noted that Azure Blob Storage supports State Lock.

State Lock

  • State locking prevents multiple Terraform operations from running simultaneously on the same state file. This is crucial when working in teams or CI/CD systems to avoid conflicts.
  • When Terraform begins to apply changes, it acquires a lock on the state file. If another Terraform process tries to make changes at the same time, it will not be able to do so until the lock is released.
  • State lock ensures consistency and prevents state file corruption. Not all Terraform backends support state locking. Some common backends that support it include Azure Blob Storage, Amazon S3 with DynamoDB, and Consul, among others.

When we run <terraform apply>, we will be able to see the state being locked:

As we can verify in the file in our Blob Storage container:

Terraform Apply

We will create a "Resource Group" resource as a test:

After running apply (and creating a test resource group), we can verify the contents of the State file:

Thanks for reading. The learning continues in the next article.