Bird
Raised Fist0
Terraformcloud~10 mins

Workspaces vs directory-based separation in Terraform - Interactive Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select the Terraform workspace named 'dev'.

Terraform
terraform workspace select [1]
Drag options to blanks, or click blank then click option'
Aprod
Btest
Cdev
Dstaging
Attempts:
3 left
💡 Hint
Common Mistakes
Using a workspace name that does not exist
Typing the workspace name incorrectly
2fill in blank
medium

Complete the code to initialize Terraform in the current directory.

Terraform
terraform [1]
Drag options to blanks, or click blank then click option'
Ainit
Bdestroy
Cplan
Dapply
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'apply' before initialization
Confusing 'plan' with 'init'
3fill in blank
hard

Fix the error in the command to create a new workspace named 'test'.

Terraform
terraform workspace [1] test
Drag options to blanks, or click blank then click option'
Aselect
Bdelete
Cnew-workspace
Dnew
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'select' instead of 'new'
Using invalid commands like 'new-workspace'
4fill in blank
hard

Fill both blanks to define a backend configuration for Terraform state stored in an S3 bucket.

Terraform
terraform {
  backend "[1]" {
    bucket = "my-terraform-state"
    region = "[2]"
  }
}
Drag options to blanks, or click blank then click option'
As3
Bgcs
Cus-west-2
Dus-east-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'gcs' which is for Google Cloud Storage
Using invalid region names
5fill in blank
hard

Fill all three blanks to create a directory-based separation with environment-specific variables in Terraform.

Terraform
variable "region" {
  type    = string
  default = "[1]"
}

provider "aws" {
  region = var.[2]
}

resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
  tags = {
    Environment = "[3]"
  }
}
Drag options to blanks, or click blank then click option'
Aus-west-1
Bregion
Cdev
Dprod
Attempts:
3 left
💡 Hint
Common Mistakes
Using mismatched variable names
Setting environment tag incorrectly

Practice

(1/5)
1. What is the main difference between Terraform workspaces and directory-based separation?
easy
A. Workspaces require separate folders; directory-based uses one folder with multiple states.
B. Workspaces store state remotely; directory-based stores state locally only.
C. Workspaces and directory-based separation are exactly the same.
D. Workspaces use one folder with multiple states; directory-based uses separate folders for each environment.

Solution

  1. Step 1: Understand workspace concept

    Workspaces allow multiple states inside the same folder by switching context.
  2. Step 2: Understand directory-based separation

    Directory-based separation uses different folders, each with its own code and state files.
  3. Final Answer:

    Workspaces use one folder with multiple states; directory-based uses separate folders for each environment. -> Option D
  4. Quick Check:

    Workspaces = one folder, multiple states [OK]
Hint: Workspaces = one folder, directory = multiple folders [OK]
Common Mistakes:
  • Confusing workspace with directory-based separation
  • Thinking workspaces require multiple folders
  • Assuming directory-based uses one state file
2. Which Terraform command correctly switches to a workspace named dev?
easy
A. terraform workspace select dev
B. terraform select workspace dev
C. terraform switch workspace dev
D. terraform workspace change dev

Solution

  1. Step 1: Recall Terraform workspace commands

    The correct command to switch workspace is terraform workspace select <name>.
  2. Step 2: Match command to options

    Only terraform workspace select dev matches the correct syntax exactly.
  3. Final Answer:

    terraform workspace select dev -> Option A
  4. Quick Check:

    Switch workspace = terraform workspace select [OK]
Hint: Use 'terraform workspace select' to switch workspaces [OK]
Common Mistakes:
  • Using incorrect command order
  • Using non-existent commands like 'switch' or 'change'
  • Confusing workspace commands with other terraform commands
3. Given this folder structure:
envs/
  ├─ dev/
  │    └─ main.tf
  └─ prod/
       └─ main.tf

and using directory-based separation, what happens if you run terraform apply inside envs/dev?
medium
A. Terraform applies changes to both dev and prod environments simultaneously.
B. Terraform applies changes only to the dev environment using its own state.
C. Terraform throws an error because state is missing.
D. Terraform applies changes to the prod environment instead.

Solution

  1. Step 1: Understand directory-based separation behavior

    Each folder has its own Terraform code and state, so running inside envs/dev affects only dev.
  2. Step 2: Analyze command effect

    terraform apply in envs/dev applies changes only to dev environment's resources.
  3. Final Answer:

    Terraform applies changes only to the dev environment using its own state. -> Option B
  4. Quick Check:

    Directory-based apply affects current folder environment [OK]
Hint: Apply runs in current folder's environment only [OK]
Common Mistakes:
  • Assuming apply affects all environments
  • Thinking state is shared across folders
  • Expecting errors due to missing state
4. You created a new workspace named staging but when running terraform apply, changes apply to the default workspace instead. What is the likely cause?
medium
A. You forgot to run terraform workspace select staging before applying.
B. The staging workspace does not exist.
C. Terraform does not support multiple workspaces.
D. You need to rename the default workspace to staging.

Solution

  1. Step 1: Check workspace usage

    Creating a workspace does not switch to it automatically; you must select it explicitly.
  2. Step 2: Identify missing command

    If you don't run terraform workspace select staging, Terraform stays in default workspace.
  3. Final Answer:

    You forgot to run terraform workspace select staging before applying. -> Option A
  4. Quick Check:

    Must select workspace before apply [OK]
Hint: Always select workspace before applying changes [OK]
Common Mistakes:
  • Assuming workspace auto-switches after creation
  • Thinking workspace names must be renamed
  • Believing Terraform lacks workspace support
5. You want to manage three environments: dev, staging, and prod. You want to keep code DRY (Don't Repeat Yourself) and share most configuration but keep states isolated. Which approach is best?
hard
A. Use one folder and switch backend configuration files for each environment.
B. Create three separate folders, each with full copies of code and state.
C. Use one folder with Terraform workspaces for each environment.
D. Use one folder and manually rename state files for each environment.

Solution

  1. Step 1: Analyze DRY and state isolation needs

    Sharing code but isolating state fits well with workspaces, which share code folder but separate states.
  2. Step 2: Compare options

    Use one folder with Terraform workspaces for each environment uses workspaces to keep one codebase and separate states per environment, avoiding code duplication.
  3. Step 3: Evaluate other options

    Create three separate folders, each with full copies of code and state duplicates code, violating DRY. Use one folder and manually rename state files for each environment is error-prone and manual. Use one folder and switch backend configuration files for each environment requires backend changes, complex to manage.
  4. Final Answer:

    Use one folder with Terraform workspaces for each environment. -> Option C
  5. Quick Check:

    Workspaces = shared code, separate states [OK]
Hint: Workspaces share code, separate states for DRY environments [OK]
Common Mistakes:
  • Duplicating code in multiple folders unnecessarily
  • Trying manual state file renaming
  • Switching backend configs frequently