2023-03-10

Terraform Workspace

What is Terraform Workspace

A Terraform workspace is a logical container for managing the state of your infrastructure resources in different environments. By default, every Terraform configuration has a single workspace called 'default'. However, you can create additional workspaces to manage multiple environments, such as development, staging, and production.

Key Concepts and Terminology

  • Workspace
    A named container for managing the state of your infrastructure resources across different environments.

  • State
    A JSON-formatted file that records the current state of your infrastructure resources managed by Terraform.

  • Environment
    A specific context in which your infrastructure resources are deployed and managed, such as development, staging, or production.

Creating and Managing Workspaces

Creating a New Workspace

To create a new workspace, use the terraform workspace new command followed by the desired workspace name. For example:

bash
$ terraform workspace new development

This command creates a new workspace called 'development', which you can use to manage your development environment.

Switching Between Workspaces

To switch between workspaces, use the terraform workspace select command followed by the workspace name:

bash
$ terraform workspace select development

This command sets the active workspace to 'development', allowing you to manage the resources within that workspace.

Deleting Workspaces

To delete a workspace, use the terraform workspace delete command followed by the workspace name:

basha
$ terraform workspace delete development

This command deletes the 'development' workspace and its associated state file.

Listing Workspaces

To list all available workspaces, use the terraform workspace list command:

bash
$ terraform workspace list

This command displays a list of all existing workspaces, with an asterisk (*) next to the currently active workspace.

Workspace-Specific Configuration

Variable and Output Handling

To handle variables and outputs in a workspace-specific manner, use the terraform.workspace interpolation. This allows you to dynamically configure resources based on the active workspace. For example:

tf
variable "instance_type" {
  type    = map(string)
  default = {
    default     = "t2.micro"
    development = "t2.small"
    production  = "m5.large"
  }
}

resource "aws_instance" "example" {
  ami           = "ami-0c94855ba95b798c7" # This is an example Amazon Linux 2 AMI ID
  instance_type = var.instance_type[terraform.workspace]

  tags = {
    Name        = "example-instance-${terraform.workspace}"
    Environment = terraform.workspace
  }
}

In this example, the instance_type variable has different values for the 'default', 'development', and 'production' workspaces. The resource's instance_type attribute uses the active workspace to determine the appropriate value.

Backend Configuration

You can also configure your backend storage to be workspace-specific. For example, using the S3 backend with a unique key for each workspace:

tf
terraform {
  backend "s3" {
    bucket = "my-terraform-state"
    key    = "state/${terraform.workspace}/terraform.tfstate"
    region = "us-west-2"
  }
}

This configuration stores the state files for each workspace in separate keys within the same S3 bucket.

Resource Tagging

Tagging resources with workspace information can be helpful for managing, auditing, and cost allocation. Using the terraform.workspace interpolation, you can add tags to your resources:

tf
resource "aws_instance" "example" {
  # ... other configuration ...

  tags = {
    Name        = "example-instance-${terraform.workspace}"
    Environment = terraform.workspace
  }
}

References

https://developer.hashicorp.com/terraform/language/state/workspaces

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!