How to store Terraform State in Azure Storage Account
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
- Prerequisites:
- Azure subscription
- Azure storage account
- Terraform installed
- Configure Backend with Terraform:
- Create a
backend.tf
file. - Specify the storage account details:
- Resource group name
- Storage account name
- Container name
- Key (optional)
- 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.
- Initialize Terraform:
- Manage Your Resources:
- Use
terraform plan
andterraform 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:
- Subscribe to Nilesh Gule’s channel (https://youtube.com/@nilesh-gule) for more Terraform tutorials.
- Leave comments suggesting future video topics.
Additional Resources
- Terraform: https://developer.hashicorp.com/terraform
- Terraform official provider for Azure: https://registry.terraform.io/providers/hashicorp/azurerm/latest
- Terraform documentation for Azure Resource Group Module: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group
- YouTube video on how to install Terraform using Homebrew: https://m.youtube.com/watch?v=oURFbTZLMl0
- YouTube video on how to provision Azure Resource Group using Terraform: https://www.youtube.com/watch?v=wsibSX00ylM
- YouTube video on how to cross reference resources in Terraform: https://m.youtube.com/watch?v=Fs7Bm0cqMsA
- YouTube video on how to manage variables in Terraform: https://www.youtube.com/watch?v=oArutYYvQ_Y
- Github repository with the source code: https://github.com/NileshGule
- Terraform videos playlist: https://m.youtube.com/playlist?list=OLAK5uy_lSeauTc6VKEW9XNFLAHaYIC7U01rWJ2Q
Originally published at https://www.handsonarchitect.com on September 20, 2024.