将Terraform CLI工作区迁移到Terraform云错误

将Terraform CLI工作区迁移到Terraform云错误,terraform,terraform-cloud,Terraform,Terraform Cloud,我正在尝试将项目的CLI工作区迁移到Terraform Cloud。我正在使用Terraform版本0.14.8并遵循官方指南 目前,该项目使用S3远程状态后端配置 terraform { backend "s3" { profile = "..." key = "..." workspace_key_prefix = "environme

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

目前,该项目使用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个工作区:

  • 快乐项目开发
  • 快乐项目分期
  • 快乐项目制作
  • 但是,我得到以下错误:

    $ 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
    
    
    我还启用了跟踪级别日志,就在它抛出错误之前,我可以看到以下内容:
    2021/03/23 10:08:03[TRACE]backend/remote:lookup workspace for orgA/

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

    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:
    

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

    我遇到了类似的问题,帮助我的是提前创建具有预期名称的空工作区,然后运行
    terraforminit

    在初始化之前,我还将.tfstate文件从远程位置复制到了项目的根目录。希望这也能对你有所帮助。

    我最后做的是

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

  • 感谢@tomasz-s回答我的问题。你的建议和我最后做的很相似
    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: