0
0
Terraformcloud~5 mins

Azure Storage backend in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you use Terraform to manage cloud resources, you need a safe place to store the information about what you created. Azure Storage backend lets you keep this information in an Azure Storage Account, so your team can share and update infrastructure safely.
When you want to share Terraform state files securely among team members using Azure cloud.
When you want to keep track of your infrastructure changes in a central place to avoid conflicts.
When you want to use Terraform with Azure resources and keep state files in Azure Storage for reliability.
When you want to enable locking to prevent multiple people from changing infrastructure at the same time.
When you want to back up your Terraform state files automatically in Azure.
Config File - main.tf
main.tf
terraform {
  backend "azurerm" {
    resource_group_name  = "example-resource-group"
    storage_account_name = "examplestoracc"
    container_name       = "tfstate"
    key                  = "terraform.tfstate"
  }
}

provider "azurerm" {
  features = {}
}

This file tells Terraform to use Azure Storage as the place to save its state file.

  • resource_group_name: The Azure group where the storage account lives.
  • storage_account_name: The name of the Azure Storage Account.
  • container_name: The blob container inside the storage account where the state file is saved.
  • key: The name of the state file.

The provider block sets up Terraform to work with Azure.

Commands
This command initializes Terraform and configures it to use the Azure Storage backend defined in the configuration file.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Successfully configured the backend "azurerm"! Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
This command shows what Terraform will do before making any changes, using the Azure Storage backend to track state.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... No changes. Infrastructure is up-to-date.
This command applies the changes to your infrastructure and saves the state file in Azure Storage automatically.
Terminal
terraform apply -auto-approve
Expected OutputExpected
Terraform used the selected providers to generate the execution plan. No changes. Infrastructure is up-to-date. Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approves the apply step without asking for confirmation.
This command lists all resources tracked in the Terraform state stored in Azure Storage.
Terminal
terraform state list
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: Azure Storage backend safely stores Terraform state files so your team can share and manage infrastructure together without conflicts.

Common Mistakes
Not creating the Azure Storage Account and container before running terraform init.
Terraform cannot find the storage location and will fail to initialize the backend.
Create the resource group, storage account, and container in Azure first, then run terraform init.
Using incorrect names for resource group, storage account, or container in the backend block.
Terraform cannot connect to the correct storage and will error out.
Double-check and use exact names as they appear in Azure.
Not running terraform init after changing backend configuration.
Terraform will not apply the new backend settings and may use local state instead.
Always run terraform init after modifying backend settings to reconfigure Terraform.
Summary
Configure Terraform backend to use Azure Storage Account for storing state files.
Run terraform init to initialize and connect to the Azure Storage backend.
Use terraform plan and terraform apply to manage infrastructure with state safely stored in Azure.
List tracked resources with terraform state list to verify state contents.