Terraform Workspaces and Environments
Using Terraform workspaces and environments to manage infrastructure
Updated April, 2024
This is part of a collection of articles covering an Introduction to Advanced Terraform, it is deliberately of a certain style, you can find out more in the first post.
Introduction
Terraform workspaces are a way to manage multiple environments such as development, staging, and production with a single Terraform configuration. This is a great way to manage your infrastructure as code (IaC) and keep your code DRY (Don't Repeat Yourself). Environments are a way to separate your infrastructure into different environments, and workspaces are a way to manage those environments. Let's get straight into it.
Terraform Workspaces and Environments
From the command line you can create a new workspace with the terraform workspace new
command. Here's an example of how you would create a new workspace called dev
:
terraform workspace new dev
When you created a new workspace, Terraform will create a new directory in the .terraform.tfstate.d
directory in your project and that workspace will become active, in the example it would be .terraform.tfstate.d/dev
. This is where Terraform will store the state for the dev
workspace. By switching between workspaces, you can use the same configuration while keeping a degree of separation between the environments and their state files.
You can list the workspaces that you have with the terraform workspace list
command. it puts an asterisk next to the currently active workspace.
To switch to a different workspace and set the environment variable you could use a command like this:
# Switch to the dev workspace
terraform workspace select dev && export TF_VAR_environment=dev
When you're ready to move to a different workspace, you can use the terraform workspace select staging && export TF_VAR_environment=staging
.
The practicality of workspaces extends beyond mere environment separation. They enable scenario testing, where you can experiment with changes in a safe, isolated environment before applying them to production. This capability is invaluable for risk mitigation and ensuring stability in your infrastructure.
Best Practices for Workspace Usage
When integrating workspaces into your Terraform workflow, consider the following best practices:
- Naming Conventions: Adopt a consistent naming strategy for workspaces to clearly identify their purpose and associated environment.
- Automation: Leverage CI/CD pipelines to automate the selection and switching of workspaces, ensuring that the correct environment variables are set for each deployment stage.
- State Management: Regularly back up state files and consider state locking to prevent concurrent executions that could lead to conflicts or inconsistencies.
Conclusion
Terraform workspaces and environments are powerful tools for managing infrastructure across multiple deployment stages. By enabling efficient separation of environments, they encourage safer deployments and offer a structured approach to infrastructure management. As you continue to explore Terraform's capabilities, incorporating these practices into your workflow will significantly enhance your infrastructure's reliability and your team's productivity.