Problem
When you perform a run in Terraform Enterprise using the remote backend, Terraform workspaces may be active in the local context. However, the name of the local Terraform workspace is not preserved when the run executes remotely in Terraform Enterprise. As a result, attempts to interpolate ${terraform.workspace} in Terraform Enterprise will always yield the value default.
Cause
The ${terraform.workspace} interpolation has different values depending on where you run Terraform and how you configure the remote backend.
When you configure the remote backend using the name option instead of the prefix option, Terraform CLI workspaces are unsupported and disabled. You can observe this behavior locally by configuring the remote backend with the name option and then attempting to create a workspace with $ terraform workspace new foo. Since workspaces are unsupported, the ${terraform.workspace} interpolation always returns the value default.
When you configure the remote backend with the prefix option, workspaces are supported. With a prefix of foo-, a new Terraform CLI workspace named bar created with $ terraform workspace new bar results in a Terraform Enterprise workspace named foo-bar. In this case, the ${terraform.workspace} interpolation produces bar locally, which is the workspace name without the prefix value.
In Terraform Enterprise, the backend configuration is always overridden to ensure that the run uses the correct remote state storage backend for that workspace. This override uses the remote backend with the name option, which causes the value of ${terraform.workspace} to always be default during the remote run.
Unfortunately, it is possible to have a local configuration with a workspace named bar, a Terraform Enterprise workspace named foo-bar, and have ${terraform.workspace} produce the value default during a remote run.
Solutions
There are two alternative approaches to access the workspace name within your configuration.
Solution 1: Use a Custom Workspace Variable
Create a Terraform variable named workspace and set its value on the Variables page for each workspace in Terraform Enterprise. This allows you to reference var.workspace to get the intended workspace name.
Solution 2: Use a Built-in Environment Variable
Use an environment variable that is present in the Terraform Enterprise worker container. You can set this variable locally to match when a local-only run (not using the remote backend) is needed.
To identify available variables, you can inspect the container environment by running the env command from a null_resource with a local-exec provisioner. The following is a trimmed example of the output.
$ terraform apply ## ... ## null_resource.example: Provisioning with 'local-exec'... ## null_resource.example (local-exec): Executing: ["/bin/sh" "-c" "env"] ATLAS_RUN_ID=run-nsYMgVXgwWDeZazK TF_VAR_ATLAS_CONFIGURATION_SLUG=randomalias/tfe_demo USER=terraform ATLAS_WORKSPACE_NAME=tfe_demo TF_VAR_ATLAS_CONFIGURATION_NAME=tfe_demo LOGNAME=terraform TF_VAR_ATLAS_WORKSPACE_SLUG=randomalias/tfe_demo PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games TF_VAR_ATLAS_ADDRESS=https://app.terraform.io ATLAS_TOKEN=<redacted> TF_VAR_ATLAS_RUN_ID=run-nsYMgVXgwWDeZazK TF_APPEND_USER_AGENT=TFE/abcd623 SHELL=/bin/bash TF_VAR_ATLAS_WORKSPACE_NAME=tfe_demo TF_VAR_TFE_RUN_ID=run-nsYMgVXgwWDeZazK ATLAS_CONFIGURATION_NAME=tfe_demo ATLAS_WORKSPACE_SLUG=randomalias/tfe_demo CHECKPOINT_DISABLE=1 TERRAFORM_CONFIG=/tmp/cli.tfrc PWD=/terraform TF_IN_AUTOMATION=1 TF_FORCE_LOCAL_BACKEND=1 ATLAS_ADDRESS=https://app.terraform.io TFE_RUN_ID=run-nsYMgVXgwWDeZazK TF_PLUGIN_MAGIC_COOKIE=<redacted> ## ...
The relevant environment variable is TF_VAR_ATLAS_WORKSPACE_NAME=tfe_demo. This naming follows the convention for setting Terraform variables via environment variables.
To make this variable available in your Terraform configuration, declare it.
variable "ATLAS_WORKSPACE_NAME" {}You can then use the variable in your configuration.
variable "ATLAS_WORKSPACE_NAME" {}
resource "null_resource" "n" {}
output "workspace" {
value = var.ATLAS_WORKSPACE_NAME
}Applying this configuration in Terraform Enterprise produces the workspace name in the output.
Outputs: workspace = "tfe_demo"
Additional Information
Please note that the use of the ATLAS_WORKSPACE_NAME variable is an implementation detail of Terraform Enterprise and is subject to change, partly due to the use of the outdated term ATLAS.