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
Recall & Review
beginner
What is a Terraform workspace?
A Terraform workspace is like a separate workspace folder where Terraform keeps its state files. It lets you manage multiple environments (like dev, test, prod) using the same configuration but with isolated states.
Click to reveal answer
beginner
Why use remote state in Terraform?
Remote state stores Terraform's state file in a shared place like cloud storage. This helps teams work together safely, avoids state conflicts, and keeps the state backed up and secure.
Click to reveal answer
intermediate
How does Terraform prevent state conflicts when using remote state?
Terraform uses locking mechanisms on remote state backends to prevent multiple people from changing the state at the same time, avoiding conflicts and corruption.
Click to reveal answer
beginner
What is the difference between local and remote state?
Local state is saved on your computer, which is simple but risky for teams. Remote state is saved in a shared service like AWS S3 or Terraform Cloud, making it safer and better for collaboration.
Click to reveal answer
intermediate
How do Terraform workspaces help manage multiple environments?
Workspaces let you switch between different state files easily without changing your code. For example, you can have a 'dev' workspace and a 'prod' workspace, each with its own infrastructure state.
Click to reveal answer
What does a Terraform workspace primarily isolate?
ATerraform configuration files
BProvider plugins
CState files
DTerraform CLI versions
✗ Incorrect
Terraform workspaces isolate state files, allowing multiple states for the same configuration.
Which of these is a benefit of using remote state?
AAllows multiple users to share state safely
BAutomatically writes Terraform code
CRuns Terraform faster locally
DRemoves the need for state files
✗ Incorrect
Remote state allows teams to share and lock state files safely.
What happens if two users try to update the remote state at the same time?
ATerraform ignores one user's changes
BTerraform merges the changes automatically
CTerraform deletes the state file
DTerraform locks the state to prevent conflicts
✗ Incorrect
Terraform uses locking to prevent simultaneous state updates.
Where is local Terraform state stored by default?
AIn a database
BOn the user's local machine
CIn Terraform Cloud
DIn a cloud storage bucket
✗ Incorrect
Local state is stored on the user's local machine by default.
How do you switch between Terraform workspaces?
AUsing the command 'terraform workspace select <name>'
BBy editing the Terraform configuration file
CBy changing the provider version
DBy deleting the state file
✗ Incorrect
You switch workspaces with 'terraform workspace select '.
Explain how Terraform workspaces help manage multiple environments.
Think about how you keep your work organized in different folders.
You got /4 concepts.
Describe the benefits of using remote state in Terraform.
Imagine sharing a single notebook safely with your team.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of Terraform workspaces?
easy
A. To store Terraform state files locally on your computer
B. To manage multiple versions of infrastructure in the same configuration
C. To write Terraform code faster using templates
D. To automatically fix errors in Terraform code
Solution
Step 1: Understand what workspaces do
Workspaces allow you to keep separate state files for the same Terraform configuration, so you can manage different environments or versions.
Step 2: Compare options
Only To manage multiple versions of infrastructure in the same configuration correctly describes this purpose. Options B, C, and D describe unrelated features.
Final Answer:
To manage multiple versions of infrastructure in the same configuration -> Option B
What happens when you run terraform workspace select dev and then terraform apply?
medium
A. Terraform stores state in S3 under key 'envs/dev/terraform.tfstate'
B. Terraform stores state in S3 under key 'envs/prod/terraform.tfstate'
C. Terraform throws an error because workspace names cannot be used in backend keys
D. Terraform stores state locally instead of S3
Solution
Step 1: Understand backend key interpolation
The backend key uses ${terraform.workspace} to dynamically set the state file path based on the current workspace.
Step 2: Apply workspace selection effect
After selecting workspace 'dev', the key becomes 'envs/dev/terraform.tfstate', so state is stored there in S3.
Final Answer:
Terraform stores state in S3 under key 'envs/dev/terraform.tfstate' -> Option A
Quick Check:
Workspace name in backend key = state path [OK]
Hint: Workspace name replaces ${terraform.workspace} in backend key [OK]
Common Mistakes:
Assuming state always stored under 'prod' key
Thinking workspace names can't be used in backend keys
Believing state is stored locally despite backend config
4. You run terraform init after changing the backend configuration, but get this error: Error: Backend reinitialization required What is the most likely cause?
medium
A. You did not run terraform init after changing backend settings
B. You have multiple state files in the same workspace
C. You switched workspaces without updating the backend
D. You changed the backend configuration but did not confirm reinitialization
Solution
Step 1: Understand backend reinitialization
Changing backend settings requires Terraform to reinitialize and confirm the changes to avoid state corruption.
Step 2: Identify cause of error
The error means Terraform detected backend changes but you did not confirm reinitialization during terraform init.
Final Answer:
You changed the backend configuration but did not confirm reinitialization -> Option D
Quick Check:
Backend change needs confirmed reinit [OK]
Hint: Confirm backend reinit after config changes with terraform init [OK]
Common Mistakes:
Ignoring the prompt to confirm backend reinitialization
Confusing workspace switch with backend reinit
Assuming multiple state files cause this error
5. You want to manage separate infrastructure for dev and prod using the same Terraform code and remote backend. Which setup is best practice?
hard
A. Use one workspace and manually rename state files in the backend
B. Create two separate Terraform configurations with different backend buckets
C. Use Terraform workspaces with backend key including ${terraform.workspace} to separate state files
D. Store all state files locally and switch workspace manually
Solution
Step 1: Understand workspace and backend usage
Workspaces let you use one configuration for multiple environments by separating state files using workspace names.
Step 2: Evaluate options for best practice
Use Terraform workspaces with backend key including ${terraform.workspace} to separate state files uses workspaces and dynamic backend keys to keep states separate and managed centrally, which is best practice.
Step 3: Reject other options
Create two separate Terraform configurations with different backend buckets duplicates code and backend unnecessarily. Use one workspace and manually rename state files in the backend risks state conflicts. Store all state files locally and switch workspace manually loses benefits of remote state.
Final Answer:
Use Terraform workspaces with backend key including ${terraform.workspace} to separate state files -> Option C
Quick Check:
Workspaces + dynamic backend key = best practice [OK]
Hint: Use workspaces with backend key for separate environment states [OK]
Common Mistakes:
Duplicating configs instead of using workspaces
Manually renaming state files causing errors
Storing state locally losing collaboration benefits