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!
What is a Terraform Workspace?
Section titled “What is a Terraform Workspace?”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
Section titled “Creating a Terraform Workspace”Creating a Terraform workspace is easy. First, you’ll need to initialize your workspace by running the following command:
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:
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.
Using Workspaces with Modules
Section titled “Using Workspaces with Modules”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.
Workspaces Work with Supported Backends
Section titled “Workspaces Work with Supported Backends”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 ins3://mybucket/dev/terraform.tfstate
. - Workspace
prod
stores state ins3://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).]
Conclusion
Section titled “Conclusion”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.