Azure

How To Save Terraform State To Distant Location In Azure

After we provision infrastructure utilizing terraform, it shops the created infrastructure state domestically in “terraform.state” domestically which is unhealthy when multiple staff is engaged on the identical undertaking. It might create battle or change the precise state of the file which can result in incorrect habits or adjustments within the provisioned infrastructure.

 

Therefore to keep away from this drawback we are able to retailer the terraform state to a distant location i.e. azure/AWS/google relying on which supplier we’re working with.

 

Retailer terraform state to file remotely gives many advantages together with offering “locks”. State file could be locked whereas it’s in use by terraform, which is a good function.

 

What’s Terraform state file?

 

Terraform shops state of the managed infrastructure and configuration. This state is utilized by Terraform to map real-world sources (provisioned in azure/aws/google) to your configuration, hold observe of metadata, and enhance efficiency for giant infrastructures. This state is saved by default in a neighborhood file named “terraform.tfstate”

 

We have to observe the beneath steps to retailer the terraform state remotely in Azure.

 

Create a storage account and container in Azure, log in to the Azure portal, and seek for “storage account” within the market to create a brand new useful resource.

 

 

 

After creating the storage account and container, let’s create a terraform configuration file “most important.tf” which is able to create a useful resource group in Azure.

In case you are unsure/new to terraform, please learn my earlier article on how a create a terraform configuration file,

  1. # Azure supplier supply ########################################  
  2. terraform {  
  3.   required_providers {  
  4.     azurerm = {  
  5.       supply  = “hashicorp/azurerm”  
  6.       model = “~>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 useful resource Supplier ########################################  
  17. supplier “azurerm” {  
  18.   options {  
  19.   
  20.   }  
  21. }  
  22.   
  23. # configure variables ########################################  
  24. variable “resource_group_name” {  
  25.   kind        = string  
  26.   description = “RG identify in Azure”  
  27.   default     = “my_terraform_rg”  
  28. }  
  29.   
  30. variable “resource_group_location” {  
  31.   kind        = string  
  32.   description = “RG location in Azure”  
  33.   default     = “centralindia”  
  34. }  
  35.   
  36. # configure sources ########################################  
  37. useful resource “azurerm_resource_group” “rg” {  
  38.   identify     = var.resource_group_name  
  39.   location = var.resource_group_location  
  40.   
  41.   tags = {  
  42.     atmosphere = “growth”  
  43.   }  
  44. }  
  45.   
  46. # Output ###############################################  
  47.   
  48. output “ResourceGroupName” {  
  49.   worth = azurerm_resource_group.rg.identify  
  50. }  

Now run the beneath Terraform instructions in PowerShell/cmd within the beneath sequence,

 

Observe

You have to have put in Azure CLI and Terraform earlier than working instructions,
  1. az login
    It should redirect you to your default browser to login into your azure account.
  2. As soon as the first step is accomplished and after profitable login, run the beneath command,

    az account set -s “Title 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. Confirm the “terraform.tfstate” file in azure below a created storage account.

    How To Save Terraform State To Remote Location In Azure

Conclusion

 

We’ve efficiently created a useful resource group and saved “terraform.tfstate” file remotely in Azure utilizing infrastructure as code. It’s a highly effective and easy-to-use device that gives extra management over infrastructure provisioning at a distant vacation spot. It may also be integrated within the Azure DevOps CI/CD pipeline simply.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button