Recently I’ve been doing some work on hobby projects and encountered the below error on my build server which was happening on the Terraform Init command.

Initializing the backend... 
Successfully configured the backend "azurerm"! Terraform will automatically 
use this backend unless the backend configuration changes. 

Error: Invalid legacy provider address 

This configuration or its associated state refers to the unqualified provider 
"azurerm". 

You must complete the Terraform 0.13 upgrade process before upgrading to later 
versions. 

I knew it was working on my developer machine ok but not on the build server and after a bit of googling around I was clear it was because id changed some of my setup on the developer machine to a newer version of terraform and how it configures the providers and but I wasn’t really that clear from the issue discussions what steps need to be taken to resolve the issue.

Current State Assessment – Dev Box

After some investigating I found that my developer machine was running version 0.14.3 of Terraform and the terraform azurerm provider was setup like following.

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "2.41.0"
    }
  }
}

provider "azurerm" {    
    skip_provider_registration = true
    features {
        key_vault {
	        recover_soft_deleted_key_vaults = true
            purge_soft_delete_on_destroy = true
        }
    }
}

In development I was using the latest at the time of terraform and also the provider and its possible id also upgraded the state file without realising it at the time.

Current State Assessment – DevOps Build

On the Build server the pipeline had already been ran at a previous point before I had started upgrading and the build pipeline uses the below snippets to setup terraform.

#Installs terraform on the build agent
    - task: TerraformInstaller@0
      displayName: Install Terraform 0.14.3
      inputs:
        terraformVersion: '0.14.3'

#Terraform Init -> Initializes terraform and installs providers
    - task: AzureCLI@2
      displayName: Terraform Init
      inputs:
        connectedServiceNameARM: '$(AzureServiceConnection)'
        scriptType: ps
        scriptLocation: inlineScript
        inlineScript: |
            #Set logging for Terraform
            $env:TF_LOG="TRACE"
            $env:TF_LOG_PATH="terraform.txt"
            #Initializes Terraform and sets up providers
            terraform init
        cwd: '$(System.ArtifactsDirectory)\$(ArtefactFolder)'

The problem was however that before id ran terraform on v0.14.3 on the build when upgrading the pipeline, I had previously been using 0.12.3 like below.

#Installs terraform on the build agent
      - task: TerraformInstaller@0
        displayName: Install Terraform 0.12.3

The problem is that on my development box I had gone through the following version upgrades of Terraform:

0.12.3 –> 0.13.4 –> 0.14.3

I think on the development box it would have automatically upgraded my state each time in a compatible way.

On the build server I have gone:

0.12.3 –> 0.14.3

When Terraform Init is ran it can not automatically upgrade the state file from v12 to v14

Steps to Correct it

Now that we know the issue, its quite easy to fix it.  I modified my yaml file in the build pipeline to look like the below.

#Installs terraform on the build agent
    - task: TerraformInstaller@0
      displayName: Install Terraform 0.13.4
      inputs:
        terraformVersion: '0.13.4'

    #Terraform upgrade to upgrade the state to version 13
    - task: AzureCLI@2
      displayName: Terraform Upgrade to version 13
      inputs:
        connectedServiceNameARM: '$(AzureServiceConnection)'
        scriptType: ps
        scriptLocation: inlineScript
        inlineScript: |
            #Set logging for Terraform
            $env:TF_LOG="TRACE"
            $env:TF_LOG_PATH="terraform.txt"
            #Initializes Terraform and sets up providers
            terraform 0.13upgrade -yes
        cwd: '$(System.ArtifactsDirectory)\$(ArtefactFolder)'

You can see I basically install version 0.13.4 and then run the terraform 0.13upgrade command with the -yes switch which will upgrade the state to version 13 of terraform.  I then allow the build to run and complete and then I remove the above tasks from the pipeline and put the install version 14 of terraform back in

#Installs terraform on the build agent
    - task: TerraformInstaller@0
      displayName: Install Terraform 0.14.3
      inputs:
        terraformVersion: '0.14.3'

Now when I run the pipeline Terraform is automatically handling the upgrade from v13 to V14.  This is discussed in more detail in the 0.14 upgrade notes linked below but it’s a combo of how the provider registration has changed combined with doing a 2 version jump on my build server.

Hopefully this will help someone else having a step by step to sort this issue out.

Useful Notes

Below are some links I found useful when troubleshooting this issue:

 

Buy Me A Coffee