Terraform 上的“无效的旧提供程序地址”错误

Posted

技术标签:

【中文标题】Terraform 上的“无效的旧提供程序地址”错误【英文标题】:"Invalid legacy provider address" error on Terraform 【发布时间】:2021-03-31 10:55:49 【问题描述】:

我正在尝试使用 terraform v0.14.3 部署一个 bitbucket 管道,以在谷歌云中创建资源。运行 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 更新为 v.0.13.0,然后运行:terraform 0.13upgrade,如本指南中所述:https://www.terraform.io/upgrade-guides/0-13.html。生成了需要 terraform 版本 >=0.13 的 versions.tf 文件,我们所需的提供程序块现在如下所示:

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 管道时仍然遇到同样的错误。有谁知道如何克服这个错误?提前致谢。

【问题讨论】:

【参考方案1】:

解决方案

如果您使用的是较新版本的 Terraform,例如 v0.14.x,您应该:

    使用replace-provider 子命令

    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.
    

    再次初始化 Terraform:

    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 . . .
    

    应该负责安装提供程序。

说明

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

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

您可以找到更多信息here。

【讨论】:

嘿@laura-h!如果此答案已解决您的问题,请考虑通过单击复选标记接受它。这向更广泛的社区表明您找到了解决方案,并为回答者和您自己赢得了一些声誉。【参考方案2】:

在我们的例子中,我们在 aws 上并且有类似的错误

...

Error: Invalid legacy provider address

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

解决的步骤是通过再次运行terraform init,检查警告并最后使用以下方法更新状态文件来确保语法升级。

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

# reinit
terraform init

特定于操作问题,如果问题仍然存在,请验证从本地和管道对存储桶位置的访问。还验证在管道中运行的 terraform 版本。根据配置,可能是远程状态文件是/不能更新。

【讨论】:

【参考方案3】:

对我来说同样的问题。我跑了:

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。在我的情况下,我有 3 个参考文献已关闭,这篇文章帮助我确定了哪些参考文献(“州所需的提供者”中的所有条目在链接中都有 -。https://github.com/hashicorp/terraform/issues/27615 我通过为每个关闭的条目运行 replace-provider 命令来纠正它,然后运行 ​​terraform init。我注意到这样做并运行 git diff,tfstate 已更新,现在使用 0.14.x terraform 而不是我之前的 0.12.x。即

terraform providers

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

【讨论】:

运行 terraform 提供程序,您可以查看是哪个提供了冲突。谢谢! terraform providers 的输出中查找内容的说明会很有用。【参考方案5】:

当您在 TF13 下时,您是否为正在运行的项目至少应用了一次状态?

根据 TF 文档:https://www.terraform.io/upgrade-guides/0-14.html

0.14 中没有(单独的)自动更新命令(就像 0.13 中一样)。升级的唯一方法是在将 TF13 移动到 14 时,至少在项目上强制状态一次。

您也可以在项目目录中尝试terraform init

【讨论】:

谢谢,这是一个很好的观点,我最初没有仔细阅读文档以意识到应用状态是必要的。但是我想这意味着我需要在本地运行它 - 在我的情况下这不是一个好的选择......希望可能有一些解决方法。 尝试将 Hashicorp/Google(在 req 提供者中)更改为 hashcorp/terraform-provider-google。我相信这是新的源链接。此外,您应该考虑更新到 google 3.5(最新版本)【参考方案6】:

我的情况是这样的

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.

【讨论】:

【参考方案7】:

说明:您的 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 文件并再次初始化项目。步骤是:

    使用相关的包名称和版本创建/编辑 required providers 块,我宁愿在versions.tf 文件上进行。

示例:

terraform 
  required_version = ">= 0.14"
  required_providers 
    aws = 
          source  = "hashicorp/aws"
          version = ">= 3.35.0"
        
      
    
    运行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 状态下切换并重新分配所需的提供程序源地址(使用 terraform state replace-provider 命令),以便我们可以告诉 terraform 如何解释旧提供程序。

terraform state replace-provider 子命令允许重新分配 提供者源地址记录在 Terraform 状态中,所以我们 可以使用此命令告诉 Terraform 如何重新解释“遗留” 提供者地址作为与 配置中的提供者源地址。

警告:terraform state replace-provider 子命令,就像所有 terraform state 子命令,将创建一个新的状态快照和 将其写入配置的后端。命令成功后 最新状态快照将使用 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
    运行 terraform init 以更新引用。

【讨论】:

【参考方案8】:

我们今天在运营环境中遇到了类似的问题。我们成功完成了terraform 0.13upgrade 命令。这确实引入了一个versions.tf 文件。

但是,仍然无法使用此设置执行terraform init,并弹出以下错误:

Error: Invalid legacy provider address

对状态文件的进一步调查显示,对于某些资源,提供程序块更新。因此,我们必须运行以下命令来完成升级过程。

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

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

【讨论】:

以上是关于Terraform 上的“无效的旧提供程序地址”错误的主要内容,如果未能解决你的问题,请参考以下文章

Terraform 学习总结—— 基于阿里云平台上的 Terraform 实战

Terraform 学习总结—— 基于阿里云平台上的 Terraform 实战

Terraform 学习总结—— 基于 AWS 云平台上的 Terraform 实战

使用 Terraform 导入 Azure 上的现有资源

Terraform 上的 Azure 应用服务自动缩放错误

加密存储在远程后端(如 GCS 存储桶)上的 Terraform 状态是不是有用?