Bird
Raised Fist0
Terraformcloud~20 mins

Creating and switching workspaces in Terraform - Practice Exercises

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Terraform Workspace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
What happens after creating a new workspace?
You run the command terraform workspace new dev in your project directory. What is the immediate effect on your Terraform environment?
AThe current workspace is renamed to 'dev'.
BA new workspace named 'dev' is created but you remain in the current workspace.
CA new workspace named 'dev' is created and automatically switched to.
DAn error occurs because you must switch manually after creating a workspace.
Attempts:
2 left
💡 Hint
Think about what Terraform does immediately after creating a workspace.
Configuration
intermediate
2:00remaining
Which command switches to an existing workspace?
You have workspaces named 'default', 'staging', and 'production'. Which command switches your current workspace to 'staging'?
Aterraform workspace select staging
Bterraform workspace new staging
Cterraform workspace use staging
Dterraform workspace switch staging
Attempts:
2 left
💡 Hint
Look for the official command to switch workspaces.
Architecture
advanced
2:00remaining
How do workspaces affect your Terraform state files?
You have multiple workspaces in your Terraform project. How does Terraform manage state files for these workspaces?
ATerraform merges all workspace states into one file automatically.
BAll workspaces share the same state file but use different prefixes.
CWorkspaces do not affect state files; all changes go to a single state file.
DEach workspace has its own separate state file stored in the backend.
Attempts:
2 left
💡 Hint
Think about isolation of environments in Terraform.
security
advanced
2:00remaining
What is a security risk when sharing Terraform workspaces?
If multiple team members share the same Terraform workspace, what is a potential security risk?
AWorkspaces automatically encrypt state files, so no risk exists.
BAccidental overwriting of state causing resource conflicts or data loss.
CTerraform prevents multiple users from accessing the same workspace.
DWorkspaces expose sensitive variables publicly by default.
Attempts:
2 left
💡 Hint
Consider what happens if multiple people change infrastructure at once.
Best Practice
expert
2:00remaining
What is the best practice for managing environments with Terraform workspaces?
You want to manage multiple environments (dev, staging, prod) using Terraform workspaces. Which approach is best?
AUse separate workspaces for each environment and keep environment-specific variables outside the main config.
BAvoid workspaces and manage environments by copying the entire Terraform project folder.
CCreate a workspace per environment but store all variables in one file without separation.
DUse a single workspace and switch variables manually before each apply.
Attempts:
2 left
💡 Hint
Think about isolation and maintainability of environments.

Practice

(1/5)
1. What is the main purpose of Terraform workspaces?
easy
A. To separate different infrastructure environments
B. To store Terraform state files locally
C. To create new Terraform modules
D. To manage user permissions

Solution

  1. Step 1: Understand workspace concept

    Terraform workspaces allow you to manage multiple distinct environments like development and production within the same configuration.
  2. Step 2: Identify the purpose

    Separating infrastructure environments helps avoid conflicts and keeps states isolated.
  3. Final Answer:

    To separate different infrastructure environments -> Option A
  4. Quick Check:

    Workspaces separate environments = A [OK]
Hint: Workspaces isolate environments like folders for projects [OK]
Common Mistakes:
  • Confusing workspaces with modules
  • Thinking workspaces manage permissions
  • Believing workspaces store files locally only
2. Which command correctly creates a new workspace named staging?
easy
A. terraform workspace create staging
B. terraform workspace new staging
C. terraform workspace add staging
D. terraform workspace select staging

Solution

  1. Step 1: Recall workspace creation syntax

    The correct command to create a new workspace is terraform workspace new <name>.
  2. Step 2: Match the command to the option

    terraform workspace new staging uses terraform workspace new staging, which is correct.
  3. Final Answer:

    terraform workspace new staging -> Option B
  4. Quick Check:

    Create workspace = terraform workspace new [OK]
Hint: Use 'terraform workspace new' to create workspaces [OK]
Common Mistakes:
  • Using 'create' instead of 'new'
  • Using 'add' which is invalid
  • Confusing 'select' with creation
3. Given the commands:
terraform workspace new test-env
tf workspace select test-env
tf workspace show

What will be the output of terraform workspace show?
medium
A. test-env
B. Error: workspace not found
C. No workspace selected
D. default

Solution

  1. Step 1: Analyze command correctness

    The first command terraform workspace new test-env is correct and creates the workspace 'test-env'.
  2. Step 2: Identify invalid commands

    The next two commands use the alias tf instead of terraform, which will cause errors unless 'tf' is defined as an alias.
  3. Step 3: Determine output of terraform workspace show

    Since tf workspace select test-env likely fails, the workspace is not switched, so terraform workspace show will output the current workspace, which remains default.
  4. Final Answer:

    default -> Option D
  5. Quick Check:

    Invalid alias causes select to fail, so workspace remains default [OK]
Hint: Use full 'terraform' command unless alias is defined [OK]
Common Mistakes:
  • Assuming 'tf' alias works by default
  • Expecting workspace switch without correct command
  • Ignoring command syntax errors
4. You run terraform workspace select prod but get an error: Workspace 'prod' does not exist. What should you do to fix this?
medium
A. Run terraform workspace new prod to create it first
B. Run terraform init to initialize the workspace
C. Delete the default workspace and retry
D. Run terraform workspace list to delete 'prod'

Solution

  1. Step 1: Understand the error

    The error means the workspace 'prod' does not exist yet, so it cannot be selected.
  2. Step 2: Create the missing workspace

    You must create it first using terraform workspace new prod before selecting it.
  3. Final Answer:

    Run terraform workspace new prod to create it first -> Option A
  4. Quick Check:

    Select workspace requires existing workspace [OK]
Hint: Create workspace before selecting it [OK]
Common Mistakes:
  • Trying to select without creating
  • Running init instead of new
  • Deleting default workspace unnecessarily
5. You want to maintain separate Terraform states for dev and prod environments using workspaces. Which sequence of commands correctly sets this up and switches to prod?
hard
A. terraform workspace new prod
terraform workspace select dev
B. terraform workspace select dev
terraform workspace select prod
C. terraform workspace new dev
terraform workspace new prod
terraform workspace select prod
D. terraform workspace new dev
terraform workspace select dev
terraform workspace new prod

Solution

  1. Step 1: Create both workspaces

    Use terraform workspace new dev and terraform workspace new prod to create separate environments.
  2. Step 2: Switch to the desired workspace

    Use terraform workspace select prod to switch to the production environment.
  3. Final Answer:

    terraform workspace new dev
    terraform workspace new prod
    terraform workspace select prod
    -> Option C
  4. Quick Check:

    Create then select workspace sequence [OK]
Hint: Create all workspaces before selecting one [OK]
Common Mistakes:
  • Selecting workspace before creating it
  • Not creating both environments
  • Switching to wrong workspace after creation