Receive error 'Invalid legacy provider address' with Terraform in Bitbucket pipeline

Laura H. picture Laura H. · Dec 21, 2020 · Viewed 8.4k times · Source

I'm trying to deploy a bitbucket pipeline using terraform v0.14.3 to create resources in google cloud. The pipeline fails with error:

Error: Invalid legacy provider address This configuration or its associated state refers to the unqualified provider "google". You must complete the Terraform 0.13 upgrade process before upgrading to later versions.

We updated our local version of terraform to v.0.13.0 and then ran: terraform 0.13upgrade as referenced in this guide: https://www.terraform.io/upgrade-guides/0-13.html. A versions.tf file was generated requiring terraform version >=0.13 and our required provider block now looks like this:

terraform {
  backend "gcs" {
    bucket      = "some-bucket"
    prefix      = "terraform/state"
    credentials = "key.json" #this is just a bitbucket pipeline variable
  }
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 2.20.0"
    }
  }
}
provider "google" {
  project     = var.project_ID
  credentials = "key.json"
  region      = var.project_region
}

We still get the same error when initiating the bitbucket pipeline. Does anyone know how to get past this error? Thanks in advance.

Answer

Hassan Mussana picture Hassan Mussana · Dec 28, 2020

Explanation

This is because Terraform only supports upgrades from one major feature upgrade at a time. Most likely the older state file in your case was written with a version earlier than 0.13 version and if you did not run terraform apply with 0.13 version, this error is to be expected because the upgrade to the 0.13 was not complete. It could've been solved had you run it (as explained in above answer).

Solution

  1. At this point (i.e. using terraform 0.14.x) you can use the replace-provider command
terraform state replace-provider "registry.terraform.io/-/google" "hashicorp/google"
  1. Now initialise again and this should take care of installing the provider:
terraform init

More information about the issue can be found here.