Every OpenTofu configuration uses a state file to track and manage resources. By default, there’s a single workspace named default. To create multiple environments (for example, development and production) from the same configuration directory, you can use workspaces to isolate state files.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Listing the Default Workspace
Run:*) indicates the currently active workspace.
Creating and Switching Workspaces
Create and switch to new workspaces:Each workspace has its own isolated state. New workspaces start empty until you run
tofu apply.Workspace Commands Reference
| Command | Description |
|---|---|
tofu workspace list | List all available workspaces |
tofu workspace new <name> | Create and switch to a new workspace |
tofu workspace select <name> | Switch to an existing workspace |
Using Workspaces in Configuration
Suppose your project directory at/root/opentofu/projects/webserver defines an AWS EC2 instance. You need:
- Development:
t2.micro - Production:
m5.large - Region:
ca-central-1 - AMI: same for both environments

variables.tf to define instance_type as a map:
main.tf, use lookup with terraform.workspace and add dynamic tags:
Verifying Workspace Values
Open the OpenTofu console to inspect workspace-specific values:Applying Changes per Workspace
In the development workspace:Local State File Organization
When using local state, OpenTofu organizes workspace-specific state underterraform.tfstate.d:
terraform.tfstate file for its workspace.
That’s it for this lesson. See you in the next one!