Default workspace in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using Terraform, the default workspace manages your infrastructure state. Understanding how operations scale with workspace use helps us know how long deployments take.
We want to see how the number of API calls changes as we use the default workspace for multiple resources.
Analyze the time complexity of the following Terraform configuration using the default workspace.
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-12345678"
instance_type = "t2.micro"
}
This code creates multiple AWS instances in the default workspace based on the count variable.
Each instance creation triggers API calls to AWS.
- Primary operation: API call to create each AWS instance.
- How many times: Once per instance, equal to the count variable.
As you increase the number of instances, the number of API calls grows directly with it.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 API calls |
| 100 | 100 API calls |
| 1000 | 1000 API calls |
Pattern observation: The number of API calls grows linearly with the number of instances.
Time Complexity: O(n)
This means the time to create resources grows directly in proportion to how many you create.
[X] Wrong: "Using the default workspace means all resources are created with a single API call regardless of count."
[OK] Correct: Each resource still requires its own API call; the workspace just manages state, not batching.
Knowing how resource count affects API calls helps you plan deployments and understand Terraform's behavior in real projects.
"What if we switched from the default workspace to multiple named workspaces? How would the time complexity change?"
Practice
default workspace in Terraform?Solution
Step 1: Understand Terraform workspaces
Terraform uses workspaces to manage different states. Thedefaultworkspace is created automatically.Step 2: Identify the role of the default workspace
Thedefaultworkspace stores the state unless you create and switch to others.Final Answer:
The main workspace Terraform uses automatically to store state -> Option DQuick Check:
Default workspace = main automatic workspace [OK]
- Thinking default workspace must be created manually
- Assuming default workspace deletes after runs
- Confusing default workspace with remote backend only
Solution
Step 1: Recall Terraform workspace commands
Common commands includelistto see all workspaces andshowto display the current one.Step 2: Identify the command to show active workspace
terraform workspace showoutputs the current active workspace name.Final Answer:
terraform workspace show -> Option BQuick Check:
Show current workspace = terraform workspace show [OK]
- Using 'terraform workspace list' to show current workspace
- Assuming 'terraform workspace current' is valid
- Confusing 'status' with workspace commands
terraform workspace list:
default * dev stagingWhich workspace is currently active?
Solution
Step 1: Understand workspace list output format
The asterisk (*) marks the currently active workspace in the list.Step 2: Identify the active workspace
The workspace with * isdev, so it is active.Final Answer:
dev -> Option CQuick Check:
Asterisk marks active workspace = dev [OK]
- Choosing 'default' because it sounds like default
- Ignoring the asterisk symbol
- Assuming no workspace is active if not obvious
terraform workspace new test but get an error: "Workspace 'test' already exists." What should you do next?Solution
Step 1: Understand the error message
The error says the workspace already exists, so you cannot create it again.Step 2: Choose the correct action
Since it exists, you should switch to it usingterraform workspace select test.Final Answer:
Run terraform workspace select test to switch to it -> Option AQuick Check:
Workspace exists? Select it, don't create [OK]
- Trying to create workspace again without switching
- Deleting workspace unnecessarily
- Running init does not fix workspace existence
prod and dev, using Terraform workspaces. Which approach correctly uses the default workspace?Solution
Step 1: Understand default workspace role
The default workspace is ready to use and often used for simpler or initial environments.Step 2: Apply best practice for environment separation
Use default fordev(development) and create/select a separate workspace forprod(production) to isolate states.Final Answer:
Use default workspace for dev and create/select prod workspace -> Option AQuick Check:
Default workspace = dev environment [OK]
- Using default for prod which risks accidental changes
- Ignoring default workspace and creating all manually
- Trying to rename default workspace (not supported)
