Skip to content

Workspaces

Terraform workspaces allow you to manage multiple instances of your infrastructure resources in the same configuration. This is particularly useful when you want to create multiple versions of the same resources, such as staging, testing, or production environments, without having to duplicate your configuration files.

In this section, we will explore how to use Terraform workspaces in your projects!

A Terraform workspace is a way to create multiple instances of the same resources within the same configuration. Each workspace has its own set of state files, which means that you can create multiple instances of the same resources without affecting the state files of other instances.

For example, you could create a workspace for your development environment and another workspace for your production environment. Each workspace would have its own set of state files, allowing you to manage your resources independently of each other.

Creating a Terraform workspace is easy. First, you’ll need to initialize your workspace by running the following command:

Terminal window
terraform workspace new my-workspace

In this example, we create a new workspace called “my-workspace”. Once you have created a new workspace, you can switch to it using the following command:

Terminal window
terraform workspace select my-workspace

This command switches your active workspace to “my-workspace”. Once you have switched to a workspace, any changes you make to your configuration will only affect the resources within that workspace.

You can also use workspaces with modules. When you create a module, you can specify a workspace-specific backend configuration, which will allow you to store the state files for each workspace separately.

Here’s an example of how to use workspaces with a module:

module "example" {
source = "./modules/example"
backend "s3" {
bucket = "my-terraform-state-bucket"
key = "example/${terraform.workspace}.tfstate"
region = "us-east-1"
}
}

In this example, we specify a workspace-specific backend configuration for our “example” module. The terraform.workspace variable allows us to store the state files for each workspace separately.

When using workspaces with supported remote backends, Terraform will automatically create and manage state files in separate locations within the backend. You don’t need to manually configure the state storage for each workspace—Terraform handles that for you.

For example, with the S3 backend:

  • Workspace dev stores state in s3://mybucket/dev/terraform.tfstate.
  • Workspace prod stores state in s3://mybucket/prod/terraform.tfstate.

In this setup, each workspace effectively maintains its own isolated state, so changes in one workspace do not impact the others.

::alert[ You can read more about support Workspace Backends [here](https://developer.hashicorp.com/terraform/language/state/workspaces#backends-supporting-multiple-workspaces).]

Terraform workspaces allow you to manage multiple instances of your infrastructure resources in the same configuration. This can be particularly useful when you want to create multiple versions of the same resources, such as staging, testing, or production environments.