A team uses Terraform Cloud workspaces to manage multiple environments. They notice that the state file for the production workspace is stored in a different backend than the development workspace. Which Terraform feature allows different workspaces to use different backends?
Trap 1: Using the -backend-config flag
Using the -backend-config flag is primarily for providing or overriding backend configuration values during a single `terraform init` command. While it can inject specific values, it does not inherently provide a dynamic mechanism for a single Terraform configuration to automatically select different backend configurations (e.g., distinct S3 buckets or key prefixes) based on the active Terraform Cloud workspace. This flag is more suited for one-off overrides rather than a scalable, workspace-aware backend management strategy.
Trap 2: Using the backend block with a workspace key
The `backend` block in Terraform HCL does not support a `workspace` argument or any similar direct key to dynamically specify different backend configurations based on the current workspace. Backend configuration within the HCL is typically static for a given `terraform init` execution. Terraform manages workspaces by isolating state within a configured backend (e.g., using a key prefix in S3 or through Terraform Cloud's native workspace isolation), rather than by allowing the backend block itself to conditionally change based on the workspace.
Trap 3: Using a remote backend type
While the `remote` backend type is specifically designed for Terraform Cloud and inherently supports the concept of workspaces by isolating state within the Terraform Cloud platform, it does not automatically use *different backend configurations* in the sense of switching to entirely distinct external backends (e.g., from an S3 bucket to an Azure Blob storage account) per workspace. The `remote` backend provides a single, unified backend experience where state isolation is managed internally by Terraform Cloud's workspace feature, not by dynamically reconfiguring the underlying backend type or its external parameters.
- A
Using the -backend-config flag
Why wrong: Using the -backend-config flag is primarily for providing or overriding backend configuration values during a single `terraform init` command. While it can inject specific values, it does not inherently provide a dynamic mechanism for a single Terraform configuration to automatically select different backend configurations (e.g., distinct S3 buckets or key prefixes) based on the active Terraform Cloud workspace. This flag is more suited for one-off overrides rather than a scalable, workspace-aware backend management strategy.
- B
Using the backend block with a workspace key
Why wrong: The `backend` block in Terraform HCL does not support a `workspace` argument or any similar direct key to dynamically specify different backend configurations based on the current workspace. Backend configuration within the HCL is typically static for a given `terraform init` execution. Terraform manages workspaces by isolating state within a configured backend (e.g., using a key prefix in S3 or through Terraform Cloud's native workspace isolation), rather than by allowing the backend block itself to conditionally change based on the workspace.
- C
Using partial configuration with a backend block that has dynamic workspace references
Partial configuration, combined with dynamic workspace references, is the correct approach. This involves defining a backend block in your HCL that uses variables or expressions (e.g., `bucket = "${var.bucket_name}"` or `key = "${terraform.workspace}/terraform.tfstate"`). During `terraform init`, these dynamic references are resolved, often by providing workspace-specific values through environment variables, Terraform Cloud workspace variables, or by leveraging the `terraform.workspace` interpolation sequence to generate unique state file paths or bucket names for each environment.
- D
Using a remote backend type
Why wrong: While the `remote` backend type is specifically designed for Terraform Cloud and inherently supports the concept of workspaces by isolating state within the Terraform Cloud platform, it does not automatically use *different backend configurations* in the sense of switching to entirely distinct external backends (e.g., from an S3 bucket to an Azure Blob storage account) per workspace. The `remote` backend provides a single, unified backend experience where state isolation is managed internally by Terraform Cloud's workspace feature, not by dynamically reconfiguring the underlying backend type or its external parameters.