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

Lau*_* H. 33 terraform bitbucket-pipelines terraform-provider-gcp

我正在尝试使用 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.
Run Code Online (Sandbox Code Playgroud)

我们将 terraform 的本地版本更新为 v.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
}
Run Code Online (Sandbox Code Playgroud)

启动 bitbucket 管道时,我们仍然遇到相同的错误。有谁知道如何克服这个错误?提前致谢。

Has*_*ana 36

解决方案

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

  1. 使用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.
    
    Run Code Online (Sandbox Code Playgroud)
  2. 再次初始化 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 . . .
    
    Run Code Online (Sandbox Code Playgroud)

    应该负责安装提供程序。

解释

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

如果你没有运行apply您升级Terraform版本之前的命令,你可以期望这个错误:从升级v0.13.xv0.14.x完整的。

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


Jim*_*Lim 13

在我们的例子中,我们在 aws 上遇到了类似的错误

...

Error: Invalid legacy provider address

This configuration or its associated state refers to the unqualified provider
"aws".
Run Code Online (Sandbox Code Playgroud)

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

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

# reinit
terraform init
Run Code Online (Sandbox Code Playgroud)

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


kjm*_*erf 7

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

terraform providers
Run Code Online (Sandbox Code Playgroud)

这给了我:

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

Providers required by state:
registry.terraform.io/-/google
Run Code Online (Sandbox Code Playgroud)

所以我跑了:

terraform state replace-provider registry.terraform.io/-/google registry.terraform.io/hashicorp/google
Run Code Online (Sandbox Code Playgroud)

这就是诀窍。


小智 2

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

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

0.14 中没有(单独)自动更新命令(与 0.13 中一样)。升级的唯一方法是在将 TF13 移动到 14 时在指挥下至少对项目强制执行一次状态。

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