Terraform Remote State Storage & Locking
Step-01: Introduction¶
- Understand Terraform Backends
- Understand about Remote State Storage and its advantages
- This state is stored by default in a local file named
terraform.tfstate, but it can also be stored remotely, which works better in a team environment. - Create Azure Storage Account to store
terraform.tfstatefile and enable backend configurations in terraform settings block
Step-02: Create Azure Storage Account¶
Step-02-01: Create Resource Group¶
- Go to Resource Groups -> Add
- Resource Group: terraform-storage-rg
- Region: East US
- Click on Review + Create
- Click on Create
Step-02-02: Create Azure Storage Account¶
- Go to Storage Accounts -> Add
- Resource Group: terraform-storage-rg
- Storage Account Name: terraformstate201 (THIS NAME SHOULD BE UNIQUE ACROSS AZURE CLOUD)
- Region: East US
- Performance: Standard
- Redundancy: Geo-Redundant Storage (GRS)
- In
Data Protection, check the optionEnable versioning for blobs - REST ALL leave to defaults
- Click on Review + Create
- Click on Create
Step-02-03: Create Container in Azure Storage Account¶
- Go to Storage Account ->
terraformstate201-> Containers ->+Container - Name: tfstatefiles
- Public Access Level: Private (no anonymous access)
- Click on Create
Step-03: Terraform Backend Configuration¶
- Reference Sub-folder: terraform-manifests
- Terraform Backend as Azure Storage Account
- Add the below listed Terraform backend block in
Terrafrom Settingsblock inc1-versions.tf
Step-04: Review Terraform Configuration Files¶
- c1-versions.tf
- c2-variables.tf
- c3-locals.tf
- c4-resource-group.tf
- c5-virtual-network.tf
- c6-linux-virtual-machine.tf
- c7-outputs.tf
- terraform.tfvars
Step-05: Test with Remote State Storage Backend¶
# Initialize Terraform
terraform init
Observation:
1. Review below message
2. Verify the Azure Storage Account and you should see terraform.tfstate file created
## Sample CLI Output
Initializing the backend...
Successfully configured the backend "azurerm"! Terraform will automatically
use this backend unless the backend configuration changes.
# Validate Terraform configuration files
terraform validate
# Review the terraform plan
terraform plan
Observation:
1. Acquiring state lock. This may take a few moments...
# Create Resources
terraform apply -auto-approve
# Verify Azure Storage Account for terraform.tfstate file
Observation:
1. Finally at this point you should see the terraform.tfstate file in Azure Storage Account.
# Access Application
http://<Public-IP>
Step-05: Storage Account Container Versioning Test¶
- Update in
c3-locals.tf - Uncomment Demo tag
- Execute Terraform Commands
# Review the terraform plan terraform plan # Create Resources terraform apply -auto-approve # Verify terraform.tfstate file in Azure Storage Account Observation: 1. New version of terraform.tfstate file will be created 2. Understand about Terraform State Locking 3. terraform.tfsate file should be in "leased" state which means no one can apply changes using terraform to Azure Resources. 4. Once the changes are completed "terraform apply", Lease State should be in "Available" state.
Step-06: Destroy Resources¶
- Destroy Resources and Verify Storage Account
terraform.tfsatefile Versioning
References¶
🎉 New Course
Ultimate DevOps Real-World Project Implementation on AWS
$15.99
$84.99
81% OFF
MARCH2026
Enroll Now on Udemy →
🎉 Offer