My Journey with Terraform and Azure – Part 2

Migrating State File 

In, my last post I showed you how to create a storage account using Terraform. In this post I’m going to show you how to change that last deployment which created a local terraform state file to using a remote terraform state file using the storage account we created.  

If you open VS Code and the directory, you deployed the storage account in you’ll see a few extra files. 

The file we are interested in is the terraform.tfstate file. I’d suggest at this stage taking a backup of that file (just copy/paste into a different directory). 

All we need to do is add this section of code to our main.tf file and rerun the terraform init, plan and apply with extra migrate option for the init stage.  

terraform { 
  backend "azurerm" { 
    resource_group_name  = "rg-starter-kit" 
    storage_account_name = "startkitstorageaccount" 
    container_name       = "tfstate" 
    key                  = "terraform.tfstate" 
  } 
} 

Resource group name, storage account name and container name are the same value as already used. The key is the name of your terraform state file. 

Do our main.tf file will now look like this: 

terraform { 
  backend "azurerm" { 
    resource_group_name  = "rg-starter-kit" 
    storage_account_name = "startkitstorageaccount" 
    container_name       = "tfstate" 
    key                  = "terraform.tfstate" 
  } 
} 
 
provider "azurerm" { 
  features {} 
} 
 
resource "azurerm_resource_group" "starter-kit" { 
  name     = "rg-starter-kit" 
  location = "uksouth" 
} 
 
resource "azurerm_storage_account" "starter-kit" { 
  name                     = "startkitstorageaccount" 
  resource_group_name      = azurerm_resource_group.starter-kit.name 
  location                 = azurerm_resource_group.starter-kit.location 
  account_tier             = "Standard" 
  account_replication_type = "LRS" 
 
  tags = { 
    environment = "starter-kit" 
  } 
} 
resource "azurerm_storage_container" "tfstate" { 
  name                 = "tfstate" 
  storage_account_name = azurerm_storage_account.starter-kit.name 
} 

Now save your file and run terraform init (don’t for get to run az login and set you azure subscription using Az account set -s “<subscription id>” 

Commands: 

Az login  

Az account set -s “<subscription id>” 

(needs to be your own subscription id)

terraform init 

You’ll see that it comes back with a warning … 

Type yes 

You will now see that the state file has been copied over successfully. You can confirm this by going to the Azure Portal and looking in your storage account > container and you will see your state file. 

Now run.  

Terraform plan 

Followed by  

Terraform apply  

This will check everything is configured correctly and will return No changes to prove it’s all been successful. 

As a part of cleaning up and an additional test you can delete your local terraform state file from VS code and rerun your init, plan and apply. Which should all run without errors and report back no changes made. 

Leave a Reply