Terraform

Migrate from AzureRM to HTTP backend

Initialize terraform project

terraform init

Fetch the TF state just for backup reasons from azurerm backend locally to file:

terraform state pull tf.tfstate > tf.tfstate

In your TF file where the backend is defined, comment the azurerm backend. For example:

  backend "azurerm" {
    resource_group_name  = "rg-XXX"
    storage_account_name = "saxxx"
    container_name       = "tf-state"
    key                  = "infra.terraform.tfstate"
  }

Add http backend, for example:

terraform {
  # https://developer.hashicorp.com/terraform/language/settings/backends/http
  backend "http" {
    address = "https://gitlab.com/api/v4/projects/<project_id>/terraform/state/<state_name>"
    lock_address = "https://gitlab.com/api/v4/projects/<project_id>/terraform/state/<state_name>/lock"
    unlock_address = "https://gitlab.com/api/v4/projects/<project_id>/terraform/state/<state_name>/lock"
    username = "<username>"
    lock_method = "POST"
    unlock_method = "DELETE"
    retry_wait_min = 5
  }
}

We won’t specify password in TF file, instead we will define it via ENV VAR like this:

export TF_HTTP_PASSWORD=<access_token>

We are ready to migrate from old backend to new one:

terraform init -migrate-state

It will ask to confirm migration

Terraform detected that the backend type changed from "azurerm" to "http".

Acquiring state lock. This may take a few moments...
Do you want to copy existing state to the new backend?
  Pre-existing state was found while migrating the previous "azurerm" backend to the
  newly configured "http" backend. No existing state was found in the newly
  configured "http" backend. Do you want to copy this state to the new "http"
  backend? Enter "yes" to copy and "no" to start with an empty state.

  Enter a value:

Confirm by typing yes, and to validate if migration was successful, validate with:

terraform plan

We should see message that there are no changes.