HashiCorp Terraform Associate Terraform Workspaces 2 — Questions and Answers
Question 1: Which command deletes a Terraform workspace named 'dev'?
- terraform workspace remove dev
- terraform workspace delete dev (Correct answer)
- terraform workspace destroy dev
- terraform workspace rm dev
Correct answer: terraform workspace delete dev
`terraform workspace delete <name>` removes the specified workspace; it will fail if the workspace has existing state unless `-force` is used.
Question 2: Can the 'default' workspace be deleted in Terraform?
- Yes, with the -force flag
- Yes, but only after running terraform destroy first
- No, the default workspace cannot be deleted (Correct answer)
- Yes, using terraform workspace delete default --confirm
Correct answer: No, the default workspace cannot be deleted
The default workspace is permanent and cannot be deleted; it always exists as the fallback workspace.
Question 3: Which is the most common use case for Terraform workspaces?
- Managing different versions of Terraform providers across teams
- Isolating state for different environments such as dev, staging, and production (Correct answer)
- Encrypting sensitive variables before storing them in state
- Running multiple Terraform configurations from a single directory simultaneously
Correct answer: Isolating state for different environments such as dev, staging, and production
Workspaces are primarily used to manage separate state files for different environments while sharing the same configuration code.
Question 4: How can you use the current workspace to choose different values in your configuration?
- By naming .tfvars files after workspaces for automatic loading
- By using terraform.workspace inside a lookup() function with a variable map (Correct answer)
- By specifying workspace-specific backend stanzas
- By using the workspace argument inside variable blocks
Correct answer: By using terraform.workspace inside a lookup() function with a variable map
A common pattern is `lookup(var.environment_config, terraform.workspace, var.default_config)` to select workspace-appropriate values.
Question 5: Which statement is true about the default workspace when using the local backend?
- The default workspace state is stored in terraform.tfstate in the working directory (Correct answer)
- All workspace states are stored in a single shared database file
- Workspaces are not supported with the local backend
- The default workspace requires its own backend block configuration
Correct answer: The default workspace state is stored in terraform.tfstate in the working directory
The default workspace's state file is stored directly as `terraform.tfstate` in the working directory, unlike other workspaces which go into `terraform.tfstate.d/`.
Question 6: Which command shows the name of the currently selected Terraform workspace?
- terraform workspace current
- terraform workspace active
- terraform workspace show (Correct answer)
- terraform workspace name
Correct answer: terraform workspace show
`terraform workspace show` prints the name of the currently active workspace to stdout.
Question 7: What best describes the relationship between Terraform workspaces and Terraform Cloud workspaces?
- They are identical features with the same capabilities
- CLI workspaces are lightweight state isolation; Terraform Cloud workspaces are richer with VCS integration, variables, and access controls (Correct answer)
- Terraform Cloud workspaces are a deprecated version of CLI workspaces
- CLI workspaces automatically sync with Terraform Cloud workspaces of the same name
Correct answer: CLI workspaces are lightweight state isolation; Terraform Cloud workspaces are richer with VCS integration, variables, and access controls
While both use the 'workspace' term, Terraform Cloud workspaces are full-featured environments with their own settings, whereas CLI workspaces only separate state.
Which command deletes a Terraform workspace named 'dev'?