Terraform Workspaces: A Simple Guide

August 11, 2023by Dhawal

Introduction

In the world of Infrastructure as Code (IaC), Terraform has emerged as a powerhouse, allowing developers and operations teams to define and manage their infrastructure in a declarative and versioned manner. One of the most powerful features of Terraform is its “Workspaces.” In this blog post, we’ll take a comprehensive look at Terraform Workspaces, exploring what they are, why they matter, and how to effectively use them to streamline your infrastructure management process.

Understanding Terraform Workspaces

Terraform Workspaces provide a mechanism to manage multiple distinct sets of infrastructure resources within the same Terraform configuration. Each workspace essentially represents an isolated environment, allowing you to maintain separate instances of your infrastructure for various stages of development, testing, and production. This is particularly beneficial when you’re dealing with multiple environments such as development, staging, and production, each with its own set of variables and configurations.

Why Terraform Workspaces Matter

Isolation and Safety: Workspaces allow you to keep your infrastructure code separate and prevent accidental interference between different environments. Changes made in one workspace won’t affect others, providing a level of isolation crucial for avoiding potential disasters in production.

Efficient Resource Management: With Workspaces, you can create, update, and manage infrastructure for different environments using a single Terraform configuration. This reduces duplication of code and ensures consistent resource provisioning across environments.

Versioned Environments: Workspaces facilitate the creation of versioned infrastructure environments. You can maintain different versions of your infrastructure code for each workspace, enabling you to roll back changes if necessary and ensuring reproducibility.

Collaboration: Teams can work independently on different workspaces without stepping on each other’s toes. This promotes collaboration among developers, operators, and other stakeholders while maintaining a clear boundary between environments.

Creating and Switching between Terraform Workspaces

• Terraform workspace list command will show the list of the workspace :

terraform workspace list

Screenshot from 2023 08 04 11 03 32

• Terraform workspace new command will create the new workspace :

terraform workspace new develop

Screenshot from 2023 08 04 11 04 16

• Terraform workspace show command will show the current workspace :

terraform workspace show

Screenshot from 2023 08 04 11 04 57

• Terraform workspace switch command will change current workspace :

terraform workspace select default

Screenshot from 2023 08 04 11 05 25

• Terraform workspace delete command will delete the existing workspace :

terraform workspace delete develop

Screenshot from 2023 08 04 11 05 46

Conclusion:

Terraform Workspaces simplify managing multiple environments. They keep things organized, safe, and adaptable. With Workspaces, your infrastructure journey becomes smoother, allowing you to focus on innovation and growth.