workspaces allow you to manage multiple environments or configurations within a single Terraform configuration directory. Each workspace maintains its own state file, enabling you to isolate environments such as development, staging, and production.

Key Concepts

  1. Default Workspace: When you initialize a Terraform project, a default workspace is created. This is where Terraform commands operate if no other workspace is specified.
  2. Custom Workspaces: You can create additional workspaces to manage separate states for different environments or configurations.

Creating and Switching Workspaces

  • Create a Workspace:
    sh

    terraform workspace new <workspace_name>
  • List Workspaces:
    sh

    terraform workspace list
  • Switch to a Workspace:
    sh

    terraform workspace select <workspace_name>
  • Delete a Workspace:
    sh

    terraform workspace delete <workspace_name>

Example Use Cases

Example 1: Managing Multiple Environments

Suppose you have a Terraform configuration that sets up infrastructure for a web application. You want to manage separate environments (development, staging, production) using workspaces.

  1. Create Workspaces:
    sh

    terraform workspace new development
    terraform workspace new staging
    terraform workspace new production
  2. Configure Variables: Use workspace-specific variable files or conditionals within your configuration files to manage environment-specific settings.
    hcl

    variable "environment" {
    description = "The environment for this configuration"
    type = string
    }

    locals {
    environment = terraform.workspace
    }

    resource "aws_instance" "web" {
    count = local.environment == "production" ? 3 : 1
    ami = "ami-0c55b159cbfafe1f0"
    instance_type = "t2.micro"
    tags = {
    Name = "web-server-${local.environment}"
    }
    }

  3. Switch and Apply Configuration:
    sh

    terraform workspace select development
    terraform apply

    terraform workspace select staging
    terraform apply

    terraform workspace select production
    terraform apply

Each workspace maintains a separate state file, ensuring that the resources for each environment are managed independently.

Example 2: Isolating Client Configurations

Imagine you are managing infrastructure for multiple clients, and you want to keep each client’s resources isolated.

  1. Create Workspaces for Clients:
    sh

    terraform workspace new clientA
    terraform workspace new clientB
  2. Client-Specific Variables: Define variables or use conditionals based on the workspace name to configure resources for each client.
    hcl

    variable "client_name" {
    description = "The client for this configuration"
    type = string
    }

    locals {
    client = terraform.workspace
    }

    resource "aws_s3_bucket" "client_bucket" {
    bucket = "myapp-${local.client}-bucket"
    acl = "private"
    }

  3. Switch and Apply Configuration:
    sh

    terraform workspace select clientA
    terraform apply

    terraform workspace select clientB
    terraform apply

This ensures that each client’s resources are managed separately, preventing any accidental overlap or interference.

Summary

Workspaces in Terraform provide a powerful way to manage multiple environments or configurations within a single codebase. By using workspaces, you can maintain separate state files and easily switch between different setups, whether for different environments (like development, staging, production) or different clients. This isolation helps to ensure that changes in one workspace do not affect resources in another, providing a more organized and secure way to manage infrastructure.