Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Terraform Default Workspace Setup
📖 Scenario: You are setting up a Terraform project to manage cloud infrastructure. Terraform uses workspaces to separate different environments like development and production. By default, Terraform uses the default workspace if no other workspace is selected.In this project, you will create a Terraform configuration that uses the default workspace to deploy a simple resource.
🎯 Goal: Create a Terraform configuration that initializes the default workspace and deploys a simple resource using it.
📋 What You'll Learn
Create a Terraform configuration file named main.tf.
Use the default workspace without creating or switching to any other workspace.
Define a simple resource (e.g., a null resource) to confirm the default workspace is used.
💡 Why This Matters
🌍 Real World
Terraform workspaces help manage multiple environments like development, staging, and production using the same configuration code but separate states.
💼 Career
Understanding Terraform workspaces is essential for cloud engineers and DevOps professionals to manage infrastructure environments safely and efficiently.
Progress0 / 4 steps
1
Create a Terraform configuration file with provider setup
Create a file named main.tf and add a terraform block with required_version set to ">= 1.0.0". Then add a provider block for null provider with no configuration.
Terraform
Hint
The terraform block sets the Terraform version requirement. The null provider is used for testing and does not require configuration.
2
Add a null_resource to the configuration
In the main.tf file, add a resource block for null_resource named example with no additional arguments.
Terraform
Hint
The null_resource is a simple resource that does nothing but can be used to test Terraform configurations.
3
Initialize Terraform to use the default workspace
Run terraform init to initialize the working directory. Then ensure the configuration uses the default workspace by not creating or switching to any other workspace in the configuration.
Terraform
Hint
Terraform uses the default workspace automatically if you do not specify any workspace configuration.
4
Add an output to confirm the default workspace is active
Add an output block named current_workspace that uses the built-in Terraform function terraform.workspace to output the name of the current workspace.
Terraform
Hint
The terraform.workspace function returns the name of the workspace Terraform is currently using.
Practice
(1/5)
1. What is the default workspace in Terraform?
easy
A. A workspace only for remote backends
B. A workspace you must create manually before use
C. A temporary workspace that deletes after each run
D. The main workspace Terraform uses automatically to store state
Solution
Step 1: Understand Terraform workspaces
Terraform uses workspaces to manage different states. The default workspace is created automatically.
Step 2: Identify the role of the default workspace
The default workspace stores the state unless you create and switch to others.
Final Answer:
The main workspace Terraform uses automatically to store state -> Option D
Quick Check:
Default workspace = main automatic workspace [OK]
Hint: Default workspace is automatic and stores state by default [OK]
Common Mistakes:
Thinking default workspace must be created manually
Assuming default workspace deletes after runs
Confusing default workspace with remote backend only
2. Which command shows the current active workspace in Terraform?
easy
A. terraform workspace list
B. terraform workspace show
C. terraform workspace current
D. terraform workspace status
Solution
Step 1: Recall Terraform workspace commands
Common commands include list to see all workspaces and show to display the current one.
Step 2: Identify the command to show active workspace
terraform workspace show outputs the current active workspace name.
Final Answer:
terraform workspace show -> Option B
Quick Check:
Show current workspace = terraform workspace show [OK]
Hint: Use 'terraform workspace show' to see active workspace [OK]
Common Mistakes:
Using 'terraform workspace list' to show current workspace
Assuming 'terraform workspace current' is valid
Confusing 'status' with workspace commands
3. Given this Terraform CLI output after running terraform workspace list:
default
* dev
staging
Which workspace is currently active?
medium
A. default
B. staging
C. dev
D. No workspace is 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 * is dev, so it is active.
Final Answer:
dev -> Option C
Quick Check:
Asterisk marks active workspace = dev [OK]
Hint: Look for * in workspace list to find active workspace [OK]
Common Mistakes:
Choosing 'default' because it sounds like default
Ignoring the asterisk symbol
Assuming no workspace is active if not obvious
4. You run terraform workspace new test but get an error: "Workspace 'test' already exists." What should you do next?
medium
A. Run terraform workspace select test to switch to it
B. Delete the existing workspace with terraform workspace delete test
C. Rename the existing workspace before creating a new one
D. Run terraform init again to fix the error
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 using terraform workspace select test.
Final Answer:
Run terraform workspace select test to switch to it -> Option A
Quick Check:
Workspace exists? Select it, don't create [OK]
Hint: Select existing workspace instead of creating it again [OK]
Common Mistakes:
Trying to create workspace again without switching
Deleting workspace unnecessarily
Running init does not fix workspace existence
5. You want to manage two environments, prod and dev, using Terraform workspaces. Which approach correctly uses the default workspace?
hard
A. Use default workspace for dev and create/select prod workspace
B. Create both prod and dev workspaces and never use default
C. Rename default workspace to prod and create dev workspace
D. Use the default workspace for prod and create/select dev 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 for dev (development) and create/select a separate workspace for prod (production) to isolate states.
Final Answer:
Use default workspace for dev and create/select prod workspace -> Option A
Quick Check:
Default workspace = dev environment [OK]
Hint: Use default for dev, create prod workspace for safety [OK]
Common Mistakes:
Using default for prod which risks accidental changes
Ignoring default workspace and creating all manually
Trying to rename default workspace (not supported)