HashiCorp Terraform Associate Terraform Workspaces 1 — Questions and Answers
Question 1: Which command creates a new Terraform workspace named 'staging'?
- terraform workspace create staging
- terraform workspace new staging (Correct answer)
- terraform workspace init staging
- terraform workspace add staging
Correct answer: terraform workspace new staging
The correct command is `terraform workspace new <name>`, which creates a new workspace and switches to it.
Question 2: Which command lists all available Terraform workspaces?
- terraform workspace show
- terraform workspace view
- terraform workspace list (Correct answer)
- terraform workspace status
Correct answer: terraform workspace list
`terraform workspace list` displays all workspaces, marking the currently selected one with an asterisk.
Question 3: What is the name of the workspace that exists by default in every Terraform configuration?
- main
- primary
- root
- default (Correct answer)
Correct answer: default
Terraform always creates a workspace named 'default' and it cannot be deleted.
Question 4: How do you reference the currently selected workspace name inside a Terraform configuration?
- var.workspace
- terraform.workspace (Correct answer)
- env.workspace
- local.workspace
Correct answer: terraform.workspace
`terraform.workspace` is a built-in expression that returns the name of the currently active workspace.
Question 5: What happens to Terraform state when you switch between workspaces?
- State is merged across workspaces
- Each workspace maintains its own separate state file (Correct answer)
- State is deleted and recreated on every switch
- State is shared across all workspaces in the same directory
Correct answer: Each workspace maintains its own separate state file
Each Terraform workspace has its own isolated state file, enabling independent management of infrastructure environments.
Question 6: Which command switches to an existing Terraform workspace named 'production'?
- terraform workspace switch production
- terraform workspace change production
- terraform workspace select production (Correct answer)
- terraform workspace use production
Correct answer: terraform workspace select production
`terraform workspace select <name>` switches the active workspace to the specified existing workspace.
Question 7: When using the local backend, where are non-default workspace state files stored?
- In a single terraform.tfstate file alongside the default state
- In a terraform.tfstate.d/ directory with a subdirectory per workspace (Correct answer)
- In separate root-level directories named after each workspace
- In the .terraform/workspaces/ hidden directory
Correct answer: In a terraform.tfstate.d/ directory with a subdirectory per workspace
With the local backend, non-default workspace states are kept in `terraform.tfstate.d/<workspace-name>/terraform.tfstate`.
Which command creates a new Terraform workspace named 'staging'?