Terraform cloud config dynamic workspace name

Solution 1:

It doesn't make sense to refer to terraform.workspace as part of the workspaces block inside a cloud block, because that block defines which remote workspaces Terraform will use and therefore dictates what final value terraform.workspace will have in the rest of your configuration.

To declare that your Terraform configuration belongs to more than one workspace in Terraform Cloud, you can assign each of those workspaces the tag "MyService" and then use the tags argument instead of the name argument:

  cloud {
    organization = "tf-organization"
    workspaces {
      tags = ["MyService"]
    }
  }

If you assign that tag to hypothetical MyService-dev and MyService-prod workspaces in Terraform Cloud and then initialize with the configuration above, Terraform will present those two workspaces for selection using the terraform workspace commands when working in this directory.

terraform.workspace will then appear as either MyService-dev or MyService-prod, depending on which one you have selected.