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,
- # Azure supplier supply ########################################
- terraform {
- required_providers {
- azurerm = {
- supply = “hashicorp/azurerm”
- model = “~>2.0”
- }
- }
- backend “azurerm” {
- resource_group_name = “terraform_remotestate_rg”
- storage_account_name = “itma63110”
- container_name = “terraform-state”
- key = “terraform.tfstate”
- }
- }
- # configure the azure useful resource Supplier ########################################
- supplier “azurerm” {
- options {
- }
- }
- # configure variables ########################################
- variable “resource_group_name” {
- kind = string
- description = “RG identify in Azure”
- default = “my_terraform_rg”
- }
- variable “resource_group_location” {
- kind = string
- description = “RG location in Azure”
- default = “centralindia”
- }
- # configure sources ########################################
- useful resource “azurerm_resource_group” “rg” {
- identify = var.resource_group_name
- location = var.resource_group_location
- tags = {
- atmosphere = “growth”
- }
- }
- # Output ###############################################
- output “ResourceGroupName” {
- worth = azurerm_resource_group.rg.identify
- }
Now run the beneath Terraform instructions in PowerShell/cmd within the beneath sequence,
Observe
-
az login
It should redirect you to your default browser to login into your azure account. -
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”
-
terraform init
-
terraform plan -out “remotestate.tfplan”
-
terraform apply “remotestate.tfplan”
-
Confirm the “terraform.tfstate” file in azure below a created storage account.
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.