0
0
Terraformcloud~20 mins

Terraform.workspace interpolation - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Terraform Workspace Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
1:30remaining
Understanding workspace interpolation in resource naming

Given the Terraform configuration below, what will be the name of the AWS S3 bucket created when the workspace is production?

Terraform
resource "aws_s3_bucket" "example" {
  bucket = "myapp-${terraform.workspace}-bucket"
  acl    = "private"
}
Amyapp-production-bucket
Bmyapp-bucket-production
Cmyapp-default-bucket
Dmyapp-bucket
Attempts:
2 left
💡 Hint

Remember that terraform.workspace returns the current workspace name exactly as it is.

Architecture
intermediate
2:00remaining
Workspace usage in environment-specific infrastructure

You want to deploy separate AWS VPCs for dev and prod environments using Terraform workspaces. Which configuration snippet correctly uses terraform.workspace to create unique VPC CIDR blocks for each workspace?

Acidr_block = terraform.workspace ? "10.0.0.0/16" : "10.1.0.0/16"
Bcidr_block = "10.${terraform.workspace}.0.0/16"
Ccidr_block = terraform.workspace == "prod" ? "10.0.0.0/16" : "10.1.0.0/16"
Dcidr_block = "10.0.0.0/16"
Attempts:
2 left
💡 Hint

Use a conditional expression to select CIDR blocks based on workspace name.

security
advanced
1:30remaining
Preventing workspace name leakage in resource tags

Consider a Terraform configuration that tags AWS resources with the current workspace name using terraform.workspace. What is a potential security risk of this practice?

AWorkspace names might reveal sensitive environment details to unauthorized users via resource metadata.
BUsing terraform.workspace in tags causes Terraform to fail during plan.
CWorkspace interpolation encrypts the workspace name, preventing any risk.
DThere is no risk; workspace names are always safe to expose.
Attempts:
2 left
💡 Hint

Think about what information resource tags can expose to users with access to cloud resources.

Configuration
advanced
1:30remaining
Correct use of terraform.workspace in backend configuration

Which backend configuration snippet correctly uses terraform.workspace to create separate state files per workspace in an S3 backend?

Akey = "state/terraform.tfstate"
Bkey = "state/terraform.workspace/terraform.tfstate"
Ckey = "state/${workspace}/terraform.tfstate"
Dkey = "state/${terraform.workspace}/terraform.tfstate"
Attempts:
2 left
💡 Hint

Use interpolation syntax to insert the workspace name dynamically.

Best Practice
expert
2:30remaining
Handling workspace interpolation in module calls for multi-environment setups

You have a Terraform module that creates resources and you want to deploy it in multiple workspaces. Which approach correctly passes the workspace name to the module to customize resource names?

Terraform
module "app" {
  source = "./modules/app"
  env    = terraform.workspace
}
AUse terraform.workspace only inside the root module; modules cannot access it.
BPass terraform.workspace as a variable to the module and use it inside the module for naming.
CHardcode the workspace name inside the module to avoid passing variables.
DSet env = "default" in the module call and ignore workspace.
Attempts:
2 left
💡 Hint

Modules can receive variables from the root module, including workspace names.