How to store Terraform State in Azure Storage Account

Nilesh Gule
2 min readSep 20, 2024

--

This blog post, based on a YouTube video tutorial, explores how to store Terraform state in a remote Azure storage account.

Why Store Terraform State Remotely?

  • Local storage creates issues when working in a team.
  • Two team members modifying the same state file can lead to conflicts.
  • Local storage deletion can cause problems.

Benefits of Remote Storage:

  • Centralized location accessible by all team members.
  • Improved collaboration and state management.

How to Store Terraform State in Azure Storage

  1. Prerequisites:
  • Azure subscription
  • Azure storage account
  • Terraform installed
  1. Configure Backend with Terraform:
  • Create a backend.tf file.
  • Specify the storage account details:
  • Resource group name
  • Storage account name
  • Container name
  • Key (optional)
  1. Set Access Key as Environment Variable:
  • Use Azure CLI to retrieve the storage account access key.
  • Set the key as an environment variable for Terraform to access the storage account.
  1. Initialize Terraform:
  2. Manage Your Resources:
  • Use terraform plan and terraform apply to plan and apply infrastructure changes.
  • The state file is created and stored in the Azure storage container.

Verifying Remote Storage:

  • Access the Azure storage account container.
  • The container should contain a terraform.tfstate file representing the Terraform state.
  • Remote storage ensures state locking and consistency.
  • Consider using Azure Key Vault for secure storage and access control of the storage account key.

Call to Action:

Additional Resources

Originally published at https://www.handsonarchitect.com on September 20, 2024.

--

--

Nilesh Gule
Nilesh Gule

Written by Nilesh Gule

Passionate about software development lives with a motto of Code with Passion and Strive for Excellence. Actively blogs at www.HandsOnArchitect.com.

No responses yet