LibraryCreating and Switching Workspaces

Creating and Switching Workspaces

Learn about Creating and Switching Workspaces as part of Terraform Infrastructure as Code Mastery

Mastering Terraform Workspaces: Creating and Switching

Terraform workspaces are a powerful feature that allows you to manage multiple distinct environments or configurations for your infrastructure using a single Terraform configuration. This is crucial for separating development, staging, and production environments, or for managing different projects with shared codebases. Understanding how to create and switch between these workspaces is fundamental to efficient Infrastructure as Code (IaC) management.

What are Terraform Workspaces?

At its core, a Terraform workspace is a named state file. When you initialize Terraform (

code
terraform init
), it creates a default workspace named
code
default
. Each workspace maintains its own independent state file, meaning that resources managed by one workspace are completely isolated from resources managed by another. This isolation is key to preventing unintended changes across environments.

Workspaces isolate Terraform state, enabling independent management of multiple environments.

Think of workspaces like separate folders for your Terraform state. Each folder holds the current status of your infrastructure for a specific environment (e.g., dev, staging, prod). This prevents accidental modifications between these environments.

Terraform uses a backend to store its state. By default, this is a local file named terraform.tfstate. When you use workspaces, Terraform dynamically selects which state file to use based on the currently active workspace. This allows you to have multiple sets of infrastructure managed by the same Terraform code, each with its own unique state, variables, and resource configurations.

Creating a New Workspace

To create a new workspace, you use the

code
terraform workspace new
command followed by the desired name for your workspace. For example, to create a workspace named
code
staging
:

What Terraform command is used to create a new workspace?

terraform workspace new

After running this command, Terraform will create a new state file for the

code
staging
workspace and automatically switch to it. You can then apply your configurations, and they will be associated with this new workspace.

Switching Between Workspaces

Once you have multiple workspaces, you'll need to switch between them to manage different environments. The command for this is

code
terraform workspace select
. For instance, to switch to the
code
staging
workspace:

You can also use

code
terraform workspace select -or-create
which will create the workspace if it doesn't exist and then select it.

Which command allows you to switch to an existing Terraform workspace?

terraform workspace select

Listing and Deleting Workspaces

To see all available workspaces, you can use

code
terraform workspace list
. The currently active workspace will be marked with an asterisk (*).

If you need to remove a workspace, you can use

code
terraform workspace delete
. Be cautious, as this command only removes the workspace's state file; it does not destroy the infrastructure managed by that workspace. You must first destroy the infrastructure using
code
terraform destroy
within that workspace before deleting it.

Warning: Deleting a Terraform workspace does NOT destroy the infrastructure it manages. Always run terraform destroy in the target workspace first.

Best Practices for Workspace Management

When using workspaces, consider these best practices:

  • Naming Conventions: Use clear and consistent naming conventions for your workspaces (e.g.,
    code
    dev
    ,
    code
    staging
    ,
    code
    prod
    ,
    code
    feature-x
    ).
  • Variable Management: Utilize Terraform's variable system to provide environment-specific values to your configurations. You can use
    code
    .tfvars
    files or environment variables.
  • Backend Configuration: Ensure your backend configuration (e.g., S3 bucket, Azure Blob Storage) is set up to handle multiple state files correctly, often by using a
    code
    key
    prefix that includes the workspace name.

The diagram illustrates the core concept of Terraform workspaces. Each 'workspace' represents an isolated state file, allowing the same Terraform configuration code to manage distinct sets of infrastructure resources for different environments like 'development', 'staging', and 'production'. The 'active workspace' dictates which state file Terraform interacts with during operations like plan or apply.

📚

Text-based content

Library pages focus on text content

Learning Resources

Terraform Workspaces - Official Documentation(documentation)

The definitive guide from HashiCorp on how Terraform workspaces function, including commands and best practices for state management.

Terraform Workspaces Tutorial by HashiCorp(tutorial)

A step-by-step tutorial that walks you through creating, switching, and managing Terraform workspaces with practical examples.

Managing Multiple Environments with Terraform Workspaces(blog)

A blog post from HashiCorp discussing the benefits and strategies for using workspaces to manage different deployment environments.

Terraform Workspaces Explained (YouTube)(video)

A visual explanation of Terraform workspaces, demonstrating their creation, switching, and impact on state management.

Terraform State Management: Workspaces and Backends(tutorial)

This tutorial covers both Terraform workspaces and backend configurations, providing a comprehensive approach to state management.

Understanding Terraform Workspaces(blog)

An in-depth look at Terraform workspaces, their purpose, and how they contribute to a robust IaC workflow.

Terraform CLI Commands Reference(documentation)

A comprehensive reference for all Terraform CLI commands, including detailed explanations for workspace commands.

Terraform Workspaces: A Practical Guide(blog)

A practical guide that delves into the practical application of Terraform workspaces for managing infrastructure.

Terraform Workspaces - A Deep Dive(blog)

A detailed article exploring the nuances and advanced usage patterns of Terraform workspaces.

Terraform Workspaces and State Management(wikipedia)

An overview of Terraform workspaces and their role in managing infrastructure state, providing a foundational understanding.