Search code examples
terraformterraform-cloud

Migrate Terraform CLI workspaces to Terraform Cloud error


I am trying to migrate a project's CLI workspaces to Terraform Cloud. I am using Terraform version 0.14.8 and following the official guide here.

$ terraform0.14.8 workspace list                 
  default
* development
  production
  staging

Currently, the project uses the S3 remote state backend configuration

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

I changed the backend configuration to:

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

    workspaces {
      prefix = "happyproject-"
    }
  }

and execute terraform0.14.8 init in order to begin the state migration process. Expected behaviour would be to create 3 workspaces in Terraform Cloud:

  1. happyproject-development
  2. happyproject-staging
  3. happyproject-production

However, I get the following error:

$ 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

I also enabled TRACE level logs and just before it throws the error I can see this: 2021/03/23 10:08:03 [TRACE] backend/remote: looking up workspace for orgA/.

Notice the empty string after orgA/ and the omission of the prefix! I am guessing that TF tries to query Terraform Cloud for the default workspace, which is an empty string, and it fails to do so. I have not been using the default workspace at all and it just appears when I am executing terraform0.14.8 init. The guide mentions:

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:

However, it never prompts me to choose a name for the default workspace. Any help would be much appreciated!


Solution

  • What I ended up doing was

    1. Created the empty workspaces in Terraform Cloud
    2. For every CLI workspace, I pointed the backend to the respective TFC workspace and executed terraform init. That way, the Terraform state was automatically migrated from S3 backend to TFC
    3. Finally, after all CLI workspaces were migrated, I used the prefix argument of the workspaces block instead of the name argument to manage the different TFC workspaces