Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using Terraform.workspace Interpolation
📖 Scenario: You are managing infrastructure for multiple environments like development and production. You want to use Terraform to create resources that change based on the current workspace.
🎯 Goal: Build a Terraform configuration that uses terraform.workspace interpolation to set resource names dynamically depending on the active workspace.
📋 What You'll Learn
Create a Terraform variable to hold a base resource name
Define a local value that combines the base name with the current workspace using terraform.workspace
Create an AWS S3 bucket resource using the combined name
Add a tag to the bucket that shows the current workspace
💡 Why This Matters
🌍 Real World
Terraform workspaces help manage multiple environments like dev, test, and prod using the same configuration but different resource names.
💼 Career
Cloud engineers use <code>terraform.workspace</code> interpolation to write reusable infrastructure code that adapts to different deployment environments.
Progress0 / 4 steps
1
Create a Terraform variable for the base resource name
Create a Terraform variable called base_name with the default value myapp.
Terraform
Hint
Use the variable block with default set to "myapp".
2
Define a local value combining base_name and terraform.workspace
Add a locals block with a local value called bucket_name that combines var.base_name and terraform.workspace separated by a dash.
Terraform
Hint
Use locals block and string interpolation with var.base_name and terraform.workspace.
3
Create an AWS S3 bucket resource using the local bucket_name
Create a resource block aws_s3_bucket named example with the bucket name set to local.bucket_name.
Terraform
Hint
Use resource "aws_s3_bucket" "example" and set bucket = local.bucket_name.
4
Add a tag to the bucket showing the current workspace
Inside the aws_s3_bucket.example resource, add a tags block with a tag Environment set to terraform.workspace.
Terraform
Hint
Add a tags block with Environment = terraform.workspace inside the resource.
Practice
(1/5)
1. What does terraform.workspace return when used inside a Terraform configuration?
easy
A. The name of the current workspace as a string
B. The current Terraform version
C. The list of all workspaces
D. The current directory path
Solution
Step 1: Understand the purpose of terraform.workspace
terraform.workspace is a built-in Terraform variable that returns the name of the workspace currently in use.
Step 2: Identify what terraform.workspace returns
It returns a string representing the workspace name, which helps differentiate environments.
Final Answer:
The name of the current workspace as a string -> Option A
Quick Check:
terraform.workspace = current workspace name [OK]
Hint: Remember: terraform.workspace always gives current workspace name [OK]
Common Mistakes:
Thinking it returns Terraform version
Confusing it with list of all workspaces
Assuming it returns file paths
2. Which of the following is the correct syntax to use terraform.workspace inside a resource name in Terraform?
easy
A. name = myapp.${terraform.workspace}
B. name = "myapp-${terraform.workspace}"
C. name = 'myapp-terraform.workspace'
D. name = "myapp.terraform.workspace"
Solution
Step 1: Understand string interpolation syntax in Terraform
Terraform uses ${} inside double quotes to insert variable values into strings.
Step 2: Identify correct usage of terraform.workspace
The correct syntax is "myapp-${terraform.workspace}" to append the workspace name.
Final Answer:
name = "myapp-${terraform.workspace}" -> Option B
Quick Check:
Use ${} inside double quotes for interpolation [OK]
Hint: Use ${terraform.workspace} inside double quotes for interpolation [OK]
Common Mistakes:
Using single quotes which disable interpolation
Missing ${} around terraform.workspace
Using dot notation without quotes
3. Given the Terraform code snippet:
output "env_name" {
value = "Current workspace is: ${terraform.workspace}"
}
If the active workspace is staging, what will be the output value?
medium
A. An error occurs because output cannot use terraform.workspace
B. "Current workspace is: default"
C. "Current workspace is: production"
D. "Current workspace is: staging"
Solution
Step 1: Understand output interpolation with terraform.workspace
The output value uses string interpolation to insert the current workspace name.
Step 2: Substitute the active workspace name
Since the active workspace is staging, the output string becomes "Current workspace is: staging".
Final Answer:
"Current workspace is: staging" -> Option D
Quick Check:
Output string includes current workspace name [OK]
Hint: Replace ${terraform.workspace} with active workspace name [OK]
Common Mistakes:
Assuming default workspace always
Thinking terraform.workspace cannot be used in outputs