CLI workspaces
One configuration, more than one state
A CLI workspace selects which set of state Terraform uses for a configuration in a working directory. Imagine a small catalog deployed for development and testing. Both deployments can use the same code and resource address, while Terraform remembers a different object for each environment. Switching workspaces selects another state; it does not move, rename or delete the objects in the previous one. The initial workspace is named default. It cannot be deleted. Workspaces are optional.
Create a workspace-aware local configuration
Use a fresh disposable directory, not an existing project. This complete configuration uses only the built-in provider and the provisional version pin inherited from earlier chapters. terraform.workspace evaluates to the selected workspace name. The input string becomes a readable label for the local record.
terraform { required_version = "= 1.14.8"}
resource "terraform_data" "catalog" { input = "catalog-${terraform.workspace}"}
output "catalog_name" { value = terraform_data.catalog.output}The default backend is local because there is no explicit backend or cloud block. For the default local backend, non-default workspace state normally lives under terraform.tfstate.d in a workspace-named subdirectory. That directory is private state, not content to commit or share. The example is a predicted lab, not a recorded execution.
Create dev, then create test
In the disposable fixture, initialize and inspect the initial selection. If dev already exists, stop and choose a fresh fixture rather than adopting it. A new workspace becomes selected immediately. list marks the active workspace, and show prints its name.
terraform initterraform workspace listterraform workspace new devterraform workspace showterraform planExpect a create action for terraform_data.catalog in dev. Review the plan before an optional terraform apply; do not approve an unexpected change. After a successful local apply, terraform output catalog_name should report catalog-dev. Next, create the independent test workspace.
terraform workspace new testterraform workspace showterraform planThe same address should be proposed for creation again, because test starts with its own empty state. Review the new plan and optionally apply only in the local fixture. Expect catalog-test. Neither plan needs a cloud account or an external provider.
Switch back and inspect ownership
After both local applies, run the following in the same fixture.
terraform workspace select devterraform workspace showterraform state listterraform output catalog_nameThe active state should contain terraform_data.catalog with output catalog-dev. The test record still exists in its own state. The address identifies an instance inside the selected state, not an object globally across all states. A normal plan in dev should propose no changes if nothing else changed. The TF_WORKSPACE environment variable can override the interactive selection, so verify the effective workspace and remove unintended overrides in the disposable lab. Workspace selection does not automatically choose a matching var file, account or region.
Delete only an empty disposable workspace
Deleting a workspace is not an infrastructure cleanup command. First select the lab-owned target and check the proposed destroy actions.
terraform workspace select testterraform workspace showterraform plan -destroyOnly if the plan shows the intended local record should you approve a local terraform destroy. Check the workspace is empty with terraform state list. Then select default and run terraform workspace delete test. Repeat the scoped process for dev. Never delete an active workspace or use forced deletion to bypass the non-empty-state guard. Forced deletion can abandon managed objects. Do not copy an existing state into a new workspace to clone infrastructure: copied bindings may point to the same objects.
When to use workspaces
CLI workspaces are convenient for short-lived, similar environments under a common trust boundary, such as private synthetic tests. Use distinct naming and independent objects if the provider has globally or account-wide unique names. Separate state alone does not guarantee distinct resources. For production with different credentials and approval rules, design stronger boundaries in the next lesson. Practice by drawing two states, each containing the same resource address but a different object identity.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
CLI workspaces — knowledge check
Review workspace selection and environment boundaries.
What changes when you select another CLI workspace?
What happens immediately after creating a new empty workspace with workspace new?
Both dev and test contain terraform_data.catalog. Is this possible?
What should you do before deleting a non-empty lab workspace?
What does terraform.workspace evaluate to?