"Invalid legacy provider address" error on Terraform

35,111

Solution 1

Solution

If you are using a newer version of Terraform, such as v0.14.x, you should:

  1. use the replace-provider subcommand

    terraform state replace-provider \
    -auto-approve \
    "registry.terraform.io/-/google" \
    "hashicorp/google"
    
    #=>
    
    Terraform will perform the following actions:
    
      ~ Updating provider:
        - registry.terraform.io/-/google
        + registry.terraform.io/hashicorp/google
    
    Changing x resources:
    
      . . .
    
    Successfully replaced provider for x resources.
    
  2. initialize Terraform again:

    terraform init
    
    #=>
    
    Initializing the backend...
    
    Initializing provider plugins...
    - Reusing previous version of hashicorp/google from the dependency lock file
    - Using previously-installed hashicorp/google vx.xx.x
    
    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try . . .
    

    This should take care of installing the provider.

Explanation

Terraform only supports upgrades from one major feature upgrade at a time. Your older state file was, more than likely, created using a version earlier than v0.13.x.

If you did not run the apply command before you upgraded your Terraform version, you can expect this error: the upgrade from v0.13.x to v0.14.x was not complete.

You can find more information here.

Solution 2

in our case, we were on aws and had similar error

...

Error: Invalid legacy provider address

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

the steps to resolve were to ensure syntax was upgraded by running terraform init again, checking the warnings and finally updating the statefile with following method.

# update provider in state file
terraform state replace-provider -- -/aws hashicorp/aws

# reinit
terraform init

specific of ops problem, if issue still occurs, verify access to the bucket location from local and from pipeline. also verify the version of terraform running in pipeline. depending on configuration it may be the remote statefile is/can not be updated.

Solution 3

Same issue for me. I ran:

terraform providers

That gave me:

Providers required by configuration:
registry.terraform.io/hashicorp/google

Providers required by state:
registry.terraform.io/-/google

So I ran:

terraform state replace-provider registry.terraform.io/-/google registry.terraform.io/hashicorp/google

That did the trick.

Solution 4

Explanation: Your terraform project contains tf.state file that is outdated and refereeing to old provider address. The Error message will present this error:

Error: Invalid legacy provider address

This configuration or its associated state refers to the unqualified provider
<some-provider>.

You must complete the Terraform <some-version> upgrade process before upgrading to later
versions.

Solution: In order to solve this issue you should change the tf.state references to link to the newer required providers, update the tf.state file and initialize the project again. The steps are:

  1. Create / Edit the required providers block with the relevant package name and version, I'd rather doing it on versions.tf file.

example:

terraform {
  required_version = ">= 0.14"
  required_providers {
    aws = {
          source  = "hashicorp/aws"
          version = ">= 3.35.0"
        }
      }
    }
  1. Run terraform providers command to present the required providers from configuration against the required providers that saved on state.

example:

   Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/aws] >= 3.35.0

Providers required by state:

    provider[registry.terraform.io/-/aws]
  1. Switch and reassign the required provider source address in the terraform state ( using terraform state replace-provider command) so we can tell terraform how to interpret the legacy provider.

The terraform state replace-provider subcommand allows re-assigning provider source addresses recorded in the Terraform state, and so we can use this command to tell Terraform how to reinterpret the "legacy" provider addresses as properly-namespaced providers that match with the provider source addresses in the configuration.

Warning: The terraform state replace-provider subcommand, like all of the terraform state subcommands, will create a new state snapshot and write it to the configured backend. After the command succeeds the latest state snapshot will use syntax that Terraform v0.12 cannot understand, so you should perform this step only when you are ready to permanently upgrade to Terraform v0.13.

example:

terraform state replace-provider registry.terraform.io/-/aws registry.terraform.io/hashicorp/aws

output:

  ~ Updating provider:
    - registry.terraform.io/-/aws
    + registry.terraform.io/hashicorp/aws
  1. run terraform init to update references.

Solution 5

To add on, I had installed terraform 0.14.6 but the state seemed to be stuck in 0.12. In my case I had 3 references that were off, this article helped me pinpoint which ones (all the entries in "Providers required by state" which had a - in the link. https://github.com/hashicorp/terraform/issues/27615 I corrected it by running the replace-provider command for each entry which was off, then running terraform init. I note doing this and running a git diff, the tfstate has been updated and now uses 0.14.x terraform instead of my previous 0.12.x. i.e.

terraform providers

terraform state replace-provider registry.terraform.io/-/azurerm registry.terraform.io/hashicorp/azurerm 
Share:
35,111

Related videos on Youtube

Laura H.
Author by

Laura H.

Updated on April 13, 2022

Comments

  • Laura H.
    Laura H. about 2 years

    I'm trying to deploy a bitbucket pipeline using terraform v0.14.3 to create resources in google cloud. after running terraform command, the pipeline fails with this 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.

  • Laura H.
    Laura H. over 3 years
    Thanks, this is a good point and I did not read the documentation closely enough initially to realize applying state was necessary. However I guess this would mean I would need to run it locally - which is not a good option in my circumstance... hoping there might be some way around this.
  • Eric E
    Eric E over 3 years
    Try changing Hashicorp/Google (in req providers) to hashicorp/terraform-provider-google. I believe this is the new source link. Additionally, you should think of updating to google 3.5 (the latest versioning)
  • Hassan Mussana
    Hassan Mussana over 3 years
    Hey @laura-h ! If this answer has solved your question, please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself.
  • nrm97
    nrm97 about 3 years
    running terraform providers you can view which one is making the conflict. Thanks!
  • C.J.
    C.J. almost 3 years
    an explanation of what to look for in the output of terraform providers would be useful.
  • prakashpoudel
    prakashpoudel about 2 years
    I ran into same issue with aws provide. @mirageglobe solution worked.
  • ActualAl
    ActualAl about 2 years
    The terraform state replace-provider registry.terraform.io/-/aws registry.terraform.io/hashicorp/aws fixed it for me with aws provider - THNX