0
0
Terraformcloud~5 mins

Default workspace in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you use Terraform to manage infrastructure, it organizes your work into workspaces. The default workspace is the first and main workspace where your infrastructure state is stored. It helps keep track of what resources Terraform manages.
When you start managing infrastructure with Terraform and have not created any additional workspaces.
When you want to keep your infrastructure state simple and do not need multiple environments like dev or prod.
When you want to run Terraform commands without specifying a workspace explicitly.
When you want to test Terraform configurations quickly without workspace switching.
When you want to understand how Terraform stores state by default.
Commands
This command shows the current workspace you are working in. By default, it should show 'default'.
Terminal
terraform workspace show
Expected OutputExpected
default
This command lists all the workspaces available in your Terraform setup. Initially, only the 'default' workspace exists.
Terminal
terraform workspace list
Expected OutputExpected
* default
This command initializes the Terraform working directory. It prepares Terraform to manage infrastructure and creates the default workspace if it does not exist.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
This command applies the Terraform configuration to create or update infrastructure in the current workspace, which is 'default' unless changed.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 15s [id=i-1234567890abcdef0] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without prompting for confirmation
Key Concept

If you remember nothing else from this pattern, remember: the default workspace is where Terraform stores your infrastructure state unless you create and switch to other workspaces.

Common Mistakes
Trying to create resources without running 'terraform init' first
Terraform needs initialization to set up the backend and plugins; without it, commands fail.
Always run 'terraform init' before applying or planning infrastructure.
Assuming multiple workspaces exist by default
Only the 'default' workspace exists initially; others must be created explicitly.
Use 'terraform workspace new <name>' to create additional workspaces.
Not checking the current workspace before applying changes
Applying in the wrong workspace can cause changes in unintended environments.
Use 'terraform workspace show' to confirm your workspace before running 'terraform apply'.
Summary
The 'default' workspace is the initial workspace Terraform uses to store state.
'terraform workspace show' and 'terraform workspace list' help you see your current and available workspaces.
'terraform init' prepares your directory and creates the default workspace if needed.