Terraform上出现“无效的遗留提供者地址”错误

63

我正在尝试使用Terraform v0.14.3部署Bitbucket管道以在Google Cloud中创建资源。运行terraform命令后,管道出现以下错误:

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.

我们将本地版本的terraform更新到0.13.0并运行了以下命令:terraform 0.13upgrade,参考了此指南:https://www.terraform.io/upgrade-guides/0-13.html。 然后生成了versions.tf文件,要求terraform版本>=0.13,并且我们所需提供程序块现在看起来像这样:

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
}

当我们启动bitbucket pipeline时,仍然会遇到相同的错误。有人知道如何解决这个问题吗?提前感谢。

11个回答

82

解决方案

如果您正在使用较新版本的Terraform,例如v0.14.x,您应该执行以下操作:

  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.

解释

Terraform 仅支持一次从一个主要功能升级到另一个的升级。您更早的状态文件很可能是使用早于 v0.13.x 版本创建的。

如果在升级 Terraform 版本之前没有运行 apply 命令,则可能会出现此错误:从 v0.13.x 升级到 v0.14.x 的升级未完成

您可以在此处找到更多信息。


嘿@laura-h!如果这个答案解决了你的问题,请考虑通过点击勾选标记来接受它。这将向更广泛的社区表明你已找到解决方案,并为回答者和你自己赢得声誉。 - Hassan Mussana

41

在我们的情况下,我们使用 AWS,并遇到了类似的错误。

...

Error: Invalid legacy provider address

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

解决步骤如下:

  1. 运行 terraform init 以确保语法已升级。
  2. 检查警告并解决它们。
  3. 最后使用以下方法更新状态文件。
# update provider in state file
terraform state replace-provider -- -/aws hashicorp/aws

# reinit
terraform init

针对ops问题,如果问题仍然存在,请验证本地和管道中对存储桶位置的访问权限。还要验证管道中运行的terraform版本。根据配置,远程状态文件可能无法更新。


1
我在使用AWS提供程序时遇到了同样的问题。@mirageglobe的解决方案奏效了。 - prakashpoudel

20

我也遇到了相同的问题。我运行了:

terraform providers

那让我有:

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

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

所以我跑了:

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

那就解决了。


4
此外,我安装了terraform 0.14.6,但状态似乎停留在0.12上。在我的情况下,有三个引用是错误的,这篇文章帮助我确定了哪些引用(即“Providers required by state”中所有链接中带有-的条目)。我通过运行每个错误条目的replace-provider命令来进行更正,然后运行terraform init。需要注意的是,运行git diff可以看到tfstate已经更新,现在使用的是0.14.x terraform而不是之前的0.12.x。链接:https://github.com/hashicorp/terraform/issues/27615
terraform providers

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

1
运行 Terraform 提供程序,您可以查看哪个提供程序正在造成冲突。谢谢! - nrm97
查看 terraform providers 输出的内容时,什么样的解释会有帮助? - C.J.

3
说明:您的 Terraform 项目包含一个过时的 tf.state 文件,其中引用了旧的提供商地址。以下是错误信息

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.
解决方案: 为了解决这个问题,你需要将tf.state的引用改为链接到新版本的提供程序,更新tf.state文件并重新初始化项目。具体步骤如下:
  1. 创建/编辑包含相关软件包名称和版本信息的必要提供程序块,建议在versions.tf文件中完成。

例子:

terraform {
  required_version = ">= 0.14"
  required_providers {
    aws = {
          source  = "hashicorp/aws"
          version = ">= 3.35.0"
        }
      }
    }
  1. 运行terraform providers命令,将配置文件中所需的提供程序与状态保存的所需提供程序进行对比。

示例:

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

Providers required by state:

    provider[registry.terraform.io/-/aws]

使用 terraform state replace-provider 命令可以切换和重新分配 terraform 状态中所需的提供程序源地址,因此我们可以使用此命令来告诉 Terraform 如何重新解释“旧版”提供程序地址,使其与配置文件中的提供程序源地址匹配并具有正确的名称空间。 注意:像所有 terraform 状态子命令一样,terraform state replace-provider 子命令将创建一个新的状态快照并将其写入已配置的后端。命令成功后,最新的状态快照将使用 Terraform v0.12 无法理解的语法,因此只有当您准备永久升级到 Terraform v0.13 时才应执行此步骤。
terraform state replace-provider registry.terraform.io/-/aws registry.terraform.io/hashicorp/aws

输出:

  ~ Updating provider:
    - registry.terraform.io/-/aws
    + registry.terraform.io/hashicorp/aws
  1. 运行 terraform init 命令以更新引用。

terraform state replace-provider registry.terraform.io/-/aws registry.terraform.io/hashicorp/aws 使用 aws provider 为我解决了问题 - 谢谢。 - ActualAl

2
我的情况是这样的。
Error: Invalid legacy provider address

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

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

为解决这个问题

remove the .terraform folder

执行以下命令:
terraform state replace-provider -- -/openstack terraform-provider-openstack/openstack

在执行此命令后,您将看到以下打印内容,请输入yes

Terraform will perform the following actions:

  ~ Updating provider:
    - registry.terraform.io/-/openstack
    + registry.terraform.io/terraform-provider-openstack/openstack

Changing 11 resources:

  openstack_compute_servergroup_v2.kubernetes_master
  openstack_networking_network_v2.kube_router
  openstack_compute_instance_v2.kubernetes_worker
  openstack_networking_subnet_v2.internal
  openstack_networking_subnet_v2.kube_router
  data.openstack_networking_network_v2.external_network
  openstack_compute_instance_v2.kubernetes_etcd
  openstack_networking_router_interface_v2.internal
  openstack_networking_router_v2.internal
  openstack_compute_instance_v2.kubernetes_master
  openstack_networking_network_v2.internal

Do you want to make these changes?
Only 'yes' will be accepted to continue.

Enter a value: yes

Successfully replaced provider for 11 resources.

2
在您使用TF13期间,您是否至少有一次为正在运行的项目申请状态?
根据TF文档:https://www.terraform.io/upgrade-guides/0-14.html 在0.14中没有像0.13中那样的自动更新命令(单独存在)。升级的唯一方法是在将TF13移动到14时至少强制对一个项目进行状态处理。
您也可以尝试在项目目录中运行terraform init

谢谢,这是一个好点子,我最初没有仔细阅读文档,以至于没有意识到应用状态是必要的。然而,我猜这意味着我需要在本地运行它——在我的情况下不是一个好选择,希望有一些别的方法可以解决这个问题。 - Laura H.
尝试将req providers中的Hashicorp/Google更改为hashicorp/terraform-provider-google。我相信这是新的源链接。此外,您应该考虑更新到google 3.5(最新版本)。 - Eric E

1
我最近在使用 Terraform Cloud 远程后端时遇到了这个问题。我们有一些旧的与 AWS 相关的工作区设置为版本 0.12.4(在云中),导致出现“无效的传统提供程序地址”错误,并拒绝使用最新的 Terraform 客户端 1.1.8 运行。
我之所以添加我的答案,是因为它比其他答案简单得多。我们没有执行以下任何操作:
  terraform providers
  terraform 0.13upgrade  
  remove the .terraform folder
  terraform state replace-provider 

相反,我们只需要:

  1. 在一个干净的文件夹中(没有本地状态,使用本地 terraform.exe 版本 0.13.7),运行 'terraform init'
  2. 对工作区中的 .tf 文件进行了一个微不足道的小修改(以确保 apply 会写入状态)
  3. 在 Terraform Cloud 中将工作区版本设置为 0.13.7
  4. 使用本地 0.13.7 terraform.exe 运行 apply - 这样就保存了新状态。
  5. 现在我们可以使用云端和本地 terraform.exe 版本 1.1.8,不再有问题了。

请注意,我们还需要更新一些与 AWS S3 相关的资源,以使用最新的提供程序让所有工作区正常工作。


1
今天我们在运营环境中遇到了类似的问题。我们成功地完成了terraform 0.13升级命令。这确实引入了一个versions.tf文件。
然而,使用这个设置执行terraform init仍然不可能,并且出现了以下错误:
Error: Invalid legacy provider address

进一步调查状态文件后发现,对于某些资源,提供程序块更新。因此,我们必须运行以下命令来完成升级过程。
terraform state replace-provider "registry.terraform.io/-/google" "hashicorp/google"

编辑 部署到下一个环境时发现这是由于条件资源引起的。为了轻松启用/禁用某些资源,我们利用count属性并使用0或1。对于那些在Terraform 0.13中未更改的count = 0的资源,提供程序没有更新。


0

我正在使用带有远程S3状态和Dynamo DB的terragrunt,但可悲的是这对我不起作用。因此,将其发布在这里可能会帮助其他人。

要使其工作,需要经过一些步骤,例如terragrunt state replace-provider对我有效。

  • 从S3下载状态文件
aws s3 cp s3://bucket-name/path/terraform.tfstate terraform.tfstate --profile profile

使用 Terraform 更换提供者。
terraform state replace-provider "registry.terraform.io/-/random" "hashicorp/random"
terraform state replace-provider "registry.terraform.io/-/aws" "hashicorp/aws"

将状态文件上传回S3,即使对我来说terragrunt state push terraform.tfstate也无效。
aws s3 cp terraform.tfstate s3://bucket-name/path/terraform.tfstate --profile profile
  • terragrunt apply
  • 该命令将抛出摘要值错误,
  • 更新 Dynamo DB 表中接收到的前一个命令的摘要值
Initializing the backend...
Error refreshing state: state data in S3 does not have the expected content.

This may be caused by unusually long delays in S3 processing a previous state
update.  Please wait for a minute or two and try again. If this problem
persists, and neither S3 nor DynamoDB are experiencing an outage, you may need
to manually verify the remote state and update the Digest value stored in the
DynamoDB table to the following value: fe2840edf8064d9225eea6c3ef2e5d1d

enter image description here

  • 最后,运行terragrunt applyenter image description here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接