How To Save Terraform State To Remote Location In Azure

When we provision infrastructure using terraform, it stores the created infrastructure state locally in “terraform.state” locally which is bad when more than one team is working on the same project. It may create conflict or change the actual state of the file which may lead to incorrect behavior or changes in the provisioned infrastructure.
 
Hence to avoid this problem we can store the terraform state to a remote location i.e. azure/AWS/google depending on which provider we are working with.
 
Store terraform state to file remotely provides many benefits including providing “locks”. State file can be locked while it is in use by terraform, which is a great feature.
 

What is Terraform state file?

 
Terraform stores state of the managed infrastructure and configuration. This state is used by Terraform to map real-world resources (provisioned in azure/aws/google) to your configuration, keep track of metadata, and improve performance for large infrastructures. This state is stored by default in a local file named "terraform.tfstate”
 

We need to follow the below steps to store the terraform state remotely in Azure.

 
Create a storage account and container in Azure, log in to the Azure portal, and search for “storage account” in the marketplace to create a new resource.
 
Please go through the article How to Create a storage account in Azure to get more detail about the storage account service.
 
How To Save Terraform State To Remote Location In Azure
 
After creating the storage account and container, let’s create a terraform configuration file “main.tf” which will create a resource group in Azure.

If you are not sure/new to terraform, please read my previous article on how a create a terraform configuration file,
  1. # Azure provider source ########################################  
  2. terraform {  
  3.   required_providers {  
  4.     azurerm = {  
  5.       source  = "hashicorp/azurerm"  
  6.       version = "~>2.0"  
  7.     }  
  8.   }  
  9.   backend "azurerm" {  
  10.     resource_group_name  = "terraform_remotestate_rg"  
  11.     storage_account_name = "itma63110"  
  12.     container_name       = "terraform-state"  
  13.     key                  = "terraform.tfstate"  
  14.   }  
  15. }  
  16. # configure the azure resource Provider ########################################  
  17. provider "azurerm" {  
  18.   features {  
  19.   
  20.   }  
  21. }  
  22.   
  23. # configure variables ########################################  
  24. variable "resource_group_name" {  
  25.   type        = string  
  26.   description = "RG name in Azure"  
  27.   default     = "my_terraform_rg"  
  28. }  
  29.   
  30. variable "resource_group_location" {  
  31.   type        = string  
  32.   description = "RG location in Azure"  
  33.   default     = "centralindia"  
  34. }  
  35.   
  36. # configure resources ########################################  
  37. resource "azurerm_resource_group" "rg" {  
  38.   name     = var.resource_group_name  
  39.   location = var.resource_group_location  
  40.   
  41.   tags = {  
  42.     environment = "development"  
  43.   }  
  44. }  
  45.   
  46. # Output ###############################################  
  47.   
  48. output "ResourceGroupName" {  
  49.   value = azurerm_resource_group.rg.name  
  50. }  
Now run the below Terraform commands in PowerShell/cmd in the below sequence,
 
Note
You must have installed Azure CLI and Terraform before running commands,
  1. az login
    It will redirect you to your default browser to login into your azure account.

  2. Once step one is completed and after successful login, run the below command,

    az account set -s “Name or ID of subscription”

  3. terraform init

    How To Save Terraform State To Remote Location In Azure
  4. terraform plan -out "remotestate.tfplan"

    How To Save Terraform State To Remote Location In Azure
  5. terraform apply "remotestate.tfplan"

    How To Save Terraform State To Remote Location In Azure

  6. Verify the “terraform.tfstate” file in azure under a created storage account.

    How To Save Terraform State To Remote Location In Azure

Conclusion

 
We have successfully created a resource group and stored “terraform.tfstate” file remotely in Azure using infrastructure as code. It is a powerful and easy-to-use tool that provides more control over infrastructure provisioning at a remote destination. It can also be incorporated in the Azure DevOps CI/CD pipeline easily.