Azure Terraform: Part 7 – Terraform State

Reading Time: 2 minutes

Greetings Terraform wizards I hope you have enjoyed the series so far. Part 7 of our journey ventures into the world of Terraform State file.

Within Visual Studio Code you may have noticed the terraform.tfstate file and wondered what it does,

The Terraform State File

In the tapestry that is Terraform, the State file stands out as your digital manuscript, chronicling the current state of your Azure environment. It is the digital blueprint upon which Azure resources are written and referenced.

At its core, it is a record keeper of information, pivotal to the orchestration and management of your Azure virtual environment. It encapsulates the configuration, dependencies, and metadata of Azure resources defined in Terraform scripts. It provides a point in time snapshot of your current Azure environment.

State File Resource Configuration

The state file hosts detailed information on each Azure resource defined within your Terraform script. If you have been following along in this series to date, the state file for our configuration will include information on,

  • Azure Resource Group FT23-RG-001 deployed within the Azure UK South region
  • Azure VNET with an address space of 10.10.0.0/16 and a Subnet 10.10.0.0/24
  • Azure NSG with a allow rule for for SSH and RDP attached to the Subnet

All future configuration changes will also be written to the state file.

State File Resource Dependencies

The state files maps out all dependencies and relationships between the Azure resource, ensuring the correct sequence of deployment of your Azure resources. In our example it will map the Subnet being dependent on the VNET being deployed first.

State File Metadata Insights

Metadata, including the Azure region and the associated resource group, is embedded within the state file. This metadata provides crucial context for the Azure resources described within your Terraform script.

State File Storage

The state file can be stored locally or remotely depending on your requirements. For collaborative working environments you will need to store the state file in a remote location like Azure Storage or a GitHub repository.

In our Terraform journey the state file is stored locally on your computer. This location will suffice for our learning journey.

However if you want to store the state file in a remote location you can store in in and Azure Storage account. This link provides detailed instructions on how to configure remote state using Azure storage..

If you want to store the state file within an GitHub repository the following link details the steps you need to complete.

I will look to cover both in future blog posts on Terraform and remote state.

Commands and the State File

The following commands interact with the state file, change the information the states file holds,

  • terraform apply: Will update the state file with the changes (additions) you have made to your environment
  • terraform destroy: Will update the state file with details of configuration that has been removed for your environment
  • terraform refresh: Refreshes the state file to reflect the configuration of your real world deployed infrastructure

Hopefully this quick overview of the Terraform state file was helpful in understanding its function, and how the data within it is maintained.

In the next chapter of our Terraform journey we will take a look at Terraform Locals.