0
0
Terraformcloud~10 mins

Terraform.workspace interpolation - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Terraform.workspace interpolation
Start Terraform run
Detect current workspace
Interpolate ${terraform.workspace}
Use workspace name in config
Apply config with workspace-specific values
End
Terraform detects the current workspace, replaces ${terraform.workspace} with its name, and applies configuration accordingly.
Execution Sample
Terraform
resource "aws_s3_bucket" "example" {
  bucket = "myapp-${terraform.workspace}-bucket"
  acl    = "private"
}
Creates an S3 bucket with a name that includes the current Terraform workspace.
Process Table
StepActionterraform.workspace ValueResulting Bucket Name
1Start Terraform apply in workspace 'default'defaultmyapp-default-bucket
2Apply configuration with interpolated bucket namedefaultmyapp-default-bucket
3Switch workspace to 'dev'devmyapp-dev-bucket
4Apply configuration with interpolated bucket namedevmyapp-dev-bucket
5Switch workspace to 'prod'prodmyapp-prod-bucket
6Apply configuration with interpolated bucket nameprodmyapp-prod-bucket
7End - workspace interpolation done for each workspace
💡 Terraform finishes applying configuration using the current workspace name for interpolation.
Status Tracker
VariableStartAfter Step 3After Step 5Final
terraform.workspacedefaultdevprodprod
bucket namemyapp-default-bucketmyapp-dev-bucketmyapp-prod-bucketmyapp-prod-bucket
Key Moments - 2 Insights
Why does the bucket name change when switching workspaces?
Because ${terraform.workspace} is replaced with the current workspace name at each apply step, as shown in execution_table rows 1, 3, and 5.
Is the bucket name fixed or dynamic?
It is dynamic and depends on the workspace, demonstrated by the variable_tracker showing bucket name changes after workspace switches.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the bucket name at Step 4?
Amyapp-default-bucket
Bmyapp-prod-bucket
Cmyapp-dev-bucket
Dmyapp-test-bucket
💡 Hint
Check the terraform.workspace value and bucket name at Step 4 in the execution_table.
At which step does terraform.workspace first change from 'default'?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the terraform.workspace column in the execution_table to find when it changes from 'default'.
If you add a new workspace 'test', what would the bucket name be when applying in that workspace?
Amyapp-test-bucket
Bmyapp-prod-bucket
Cmyapp-default-bucket
Dmyapp-dev-bucket
💡 Hint
Refer to how bucket names are formed by combining 'myapp-' with the current workspace name from variable_tracker.
Concept Snapshot
Terraform.workspace interpolation:
- Use ${terraform.workspace} to get current workspace name.
- Workspace name changes with 'terraform workspace select'.
- Interpolation updates config dynamically per workspace.
- Useful for naming resources uniquely per environment.
- Always run 'terraform workspace select' before apply.
Full Transcript
Terraform uses the special variable terraform.workspace to know which workspace is active. When you write ${terraform.workspace} in your configuration, Terraform replaces it with the current workspace name during apply. This lets you create resources with names that change depending on the workspace, like different buckets for dev, prod, or default. The execution table shows how the bucket name changes as you switch workspaces and apply the config. The variable tracker confirms terraform.workspace and bucket name values update step by step. This helps keep environments separate and organized.