0
0
TerraformHow-ToBeginner · 3 min read

How to Create Workspace in Terraform: Simple Guide

Use the terraform workspace new <name> command to create a new workspace in Terraform. Workspaces let you manage multiple distinct states, like separate environments, within the same configuration.
📐

Syntax

The basic command to create a workspace is terraform workspace new <workspace_name>. Here:

  • terraform workspace is the command group for workspace operations.
  • new tells Terraform to create a new workspace.
  • <workspace_name> is the name you choose for your workspace.

Workspaces help you keep different infrastructure states separate, like development and production.

bash
terraform workspace new myworkspace
Output
Created and switched to workspace "myworkspace". You are now using workspace "myworkspace".
💻

Example

This example shows how to create a new workspace called dev and switch to it. This keeps your development environment separate from others.

bash
terraform workspace new dev
terraform workspace list
Output
* dev default
⚠️

Common Pitfalls

Common mistakes when creating workspaces include:

  • Trying to create a workspace without initializing Terraform first (terraform init).
  • Confusing workspaces with separate Terraform configurations; workspaces share the same config but separate state.
  • Not switching to the new workspace after creation, which means changes apply to the wrong environment.

Always run terraform init before workspace commands and confirm your current workspace with terraform workspace show.

bash
terraform workspace new dev
terraform workspace show

# Wrong: Not switching workspace
terraform apply

# Right: Switch workspace before apply
terraform workspace select dev
terraform apply
📊

Quick Reference

CommandDescription
terraform workspace new Create a new workspace with the given name
terraform workspace listList all existing workspaces
terraform workspace select Switch to an existing workspace
terraform workspace showShow the current workspace name

Key Takeaways

Create a workspace using 'terraform workspace new ' to isolate state.
Always run 'terraform init' before managing workspaces.
Switch to the workspace with 'terraform workspace select ' before applying changes.
Workspaces share configuration but keep separate state files.
Use 'terraform workspace list' and 'terraform workspace show' to manage and verify workspaces.