terraform workspace new
Theterraform workspace new command creates a new workspace and switches to it.
Usage
Description
This command creates a new workspace with the specified name and automatically switches to it. Each workspace maintains its own state file, allowing you to manage multiple distinct infrastructure configurations from the same Terraform code. When a workspace is created, it starts with an empty state. You can optionally copy state from an existing state file using the-state option.
Arguments
NAME- (Required) The name of the workspace to create. Must be a valid workspace name containing only URL-safe characters with no path separators.
Options
-
-lock=true- Lock the state file during the operation. Set tofalseto disable locking. Default:true -
-lock-timeout=0s- Duration to wait for a state lock to be released before timing out. Accepts values like10sor5m. Default:0s(no retry) -
-state=path- Path to an existing state file to copy into the new workspace. This allows you to create a new workspace based on existing infrastructure state.
Examples
Create a New Workspace
Create a Workspace from an Existing State File
staging and initializes it with the state from terraform.tfstate.backup.
Create with Custom Lock Timeout
Behavior Notes
Workspace Already Exists
If you attempt to create a workspace that already exists, the command will fail:Workspace Override
You cannot create a workspace when theTF_WORKSPACE environment variable is set to a different workspace name. The command will fail with:
TF_WORKSPACE is set to the same name as the workspace you’re creating, the command will succeed.
Invalid Workspace Names
Workspace names must:- Not be empty
- Contain only URL-safe characters
- Not contain path separators
- Not require URL encoding
State File Copying
When using the-state option:
- The new workspace is created first
- State locking is acquired (unless
-lock=false) - The state file is read and validated
- The state is written to the new workspace
- The lock is released
Exit Codes
0- Success1- Error (e.g., workspace already exists, invalid name, failed to load backend, state file errors)
Related Commands
- terraform workspace list - List all workspaces
- terraform workspace select - Switch to a different workspace
- terraform workspace show - Show the current workspace name
- terraform workspace delete - Delete a workspace