【问题标题】:Migrate Terraform CLI workspaces to Terraform Cloud error将 Terraform CLI 工作区迁移到 Terraform Cloud 错误
【发布时间】:2021-06-19 21:19:17
【问题描述】:

我正在尝试将项目的 CLI 工作区迁移到 Terraform Cloud。我正在使用 Terraform 版本 0.14.8 并遵循官方指南 here

$ terraform0.14.8 workspace list                 
  default
* development
  production
  staging

目前项目使用S3远程状态后端配置

terraform {
  backend "s3" {
    profile              = "..."
    key                  = "..."
    workspace_key_prefix = "environments"
    region               = "us-east-1"
    bucket               = "terraform-state-bucketA"
    dynamodb_table       = "terraform-state-bucketA"
    encrypt              = true
  }

我把后端配置改成:

  backend "remote" {
    hostname     = "app.terraform.io"
    organization = "orgA"

    workspaces {
      prefix = "happyproject-"
    }
  }

并执行terraform0.14.8 init 以开始状态迁移过程。预期行为是在 Terraform Cloud 中创建 3 个工作区:

  1. 项目开发愉快
  2. happyproject-staging
  3. happyproject-生产

但是,我收到以下错误:

$ terraform0.14.8 init                           
Initializing modules...

Initializing the backend...
Backend configuration changed!

Terraform has detected that the configuration specified for the backend
has changed. Terraform will now check for existing state in the backends.

Terraform detected that the backend type changed from "s3" to "remote".

Error: Error looking up workspace

Workspace read failed: invalid value for workspace

我还启用了 TRACE 级别日志,在它引发错误之前我可以看到:2021/03/23 10:08:03 [TRACE] backend/remote: looking up workspace for orgA/

注意orgA/ 后面的空字符串和前缀的省略!我猜 TF 尝试向 Terraform Cloud 查询默认工作区,这是一个空字符串,但它没有这样做。 我根本没有使用default 工作区,它只是在我执行terraform0.14.8 init 时出现。该指南提到:

Some backends, including the default local backend, allow a special default workspace that doesn't have a specific name. If you previously used a combination of named workspaces and the special default workspace, the prompt will next ask you to choose a new name for the default workspace, since Terraform Cloud doesn't support unnamed workspaces:

但是,它从不提示我为默认工作区选择名称。任何帮助将不胜感激!

【问题讨论】:

    标签: terraform terraform-cloud


    【解决方案1】:

    我最终做的是

    1. 在 Terraform Cloud 中创建了空工作区
    2. 对于每个 CLI 工作区,我将后端指向相应的 TFC 工作区并执行 terraform init。这样一来,Terraform 状态就会自动从 S3 后端迁移到 TFC
    3. 最后,在迁移所有 CLI 工作区后,我使用工作区块的 prefix 参数而不是 name 参数来管理不同的 TFC 工作区

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题,帮助我的是提前创建具有预期名称的空工作区,然后运行 ​​terraform init。 在进行初始化之前,我还将 .tfstate 文件从远程位置复制到项目的根目录。希望这对您也有帮助。

      【讨论】:

      • 感谢@tomasz-s 回答我的问题。你的建议和我最终做的很相似
      猜你喜欢
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 2019-11-04
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      相关资源
      最近更新 更多