0
0
TerraformConceptBeginner · 3 min read

What is Workspace in Terraform: Explanation and Example

A workspace in Terraform is a way to manage multiple distinct states of your infrastructure using the same configuration. It lets you switch between different environments like development and production without changing your code.
⚙️

How It Works

Think of a workspace in Terraform like different folders for your projects, but all using the same blueprint. Each workspace keeps its own record of the resources it manages, called the state. This means you can have one workspace for testing and another for live use, and they won't mix up their data.

When you switch workspaces, Terraform changes which state file it uses. This is like changing the lens through which Terraform sees your infrastructure. It helps keep environments separate and safe, so changes in one don't affect the others.

💻

Example

This example shows how to create and switch between workspaces in Terraform.

bash
terraform workspace list
terraform workspace new dev
terraform workspace select dev
terraform workspace show
Output
* default dev dev
🎯

When to Use

Use workspaces when you want to manage multiple environments with the same Terraform code. For example, you might have separate workspaces for development, staging, and production. This keeps each environment's resources and state isolated.

Workspaces are helpful when you want to test changes safely before applying them to live infrastructure. They are also useful for teams working on different versions of infrastructure without conflicts.

Key Points

  • Workspaces isolate Terraform state files for different environments.
  • They allow using the same configuration for multiple setups.
  • Switching workspaces changes which infrastructure Terraform manages.
  • Good for managing dev, test, and production environments separately.

Key Takeaways

Terraform workspaces let you manage multiple environments with one configuration.
Each workspace keeps its own state to avoid mixing resources.
Switch workspaces to change which environment you work on.
Use workspaces to safely test and deploy infrastructure changes.
Workspaces help teams avoid conflicts by isolating environments.