Terraform Workspaces provide the ability to have separate state files for environments. Separating the state files provides isolation between multiple environments. While working in one workspace, changes are committed to the state file of a single environment. Therefore, they do not affect resources in another workspace. When managing separate environments and large deployments, this separation is critical for peace of mind.
What is a Terraform Workspace?
We can think of workspaces as a layer of isolation for Terraform state files. Every workspace has its state file. All modifications applied in a particular workspace will never affect resources managed by another. Workspaces are the key to managing multiple independent environments using a single Terraform project.
We can use the current workflow’s name in our configuration files by using the
terraform.workspace variable when using workspaces in Terraform.
A fairly common use case is to create scoped names for resources. For example, consider using Terraform to provision an Azure Resource Group. Azure enforces a unique name constraint on Resource Groups in the context of your Azure subscription. (We can have only one Resource Group with the name cool-product). By adding the name of the current workspace (e.g. dev, test, or prod), we enforce the unique name constraint.
Terraform Workspaces in Practice
Creating Workspaces for environments
Every Terraform project comes with a workspace out of the box. When we execute the
terraform init, Terraform implicitly creates a new workspace. The name of this workspace is always
default. However, we can quickly create a custom workspace using the
terraform workspace new command:
Create a workspace from an existing state file
You can bring in workspaces also for existing Terraform projects. For example, if we already have a state file, use the state option to copy the existing state into the new workspace.
Display the current workspace
To know which workspace we are currently interacting with, execute
terraform workspace show. Terraform will quickly print the name of the current workspace.
List all available workspaces
We can list the available workspaces using the
How to switch workspace
Switching to a different namespace is super easy in Terraform. Just execute the
terraform workspace select command, followed by the name of the desired workspace.
Remove a workspace
Sometimes, we want to remove a specific workspace. The sub-command
delete is responsible for that. But, first, delete the
staging environment and check the list of remaining workspaces. There is only one exception; we can not delete the
Creating the Infrastructure
Finally, let’s get hands-on and create the infrastructure from our written code. Initially, we need to initialize the project using the
init subcommand, like so:
The init command will install the required dependencies for our project like the cloud provider, plugins, etc.
Next up, we need to switch to the correct environment. For example, let’s change the
dev environment for illustration:
Next, let’s run a plan of our infrastructure to see what resources will be created once we apply the code, there however is an extra variable we need to pass in called the
-var-file. This argument takes the
tfvars file based on the environment we selected:
Finally, let’s apply the infrastructure using the
Dive Deeper: Recommended Reads
Expand your knowledge of Infrastructure as Code and Terraform with our insightful collection of articles! Dive into a range of topics that will help you master the art of managing infrastructure:
- Terraform Best Practices: Learn the most effective ways to use Terraform in your projects.
- Building highly available VMSS on Azure using Terraform Modules: Create scalable and highly available virtual machine scale sets on Azure.
- Building an Elasticache cluster on AWS using Terraform Modules: Harness the power of AWS Elasticache with Terraform.
- Demystifying Terraform Modules: Understand the ins and outs of Terraform modules.
- Building an Nginx web server on Azure using Terraform: Deploy a reliable Nginx web server on Azure.
- Building an Nginx web server on AWS using Terraform: Set up an Nginx web server on AWS with Terraform.
- Introduction to Infrastructure as Code (IaC): Get started with Infrastructure as Code and grasp the fundamentals.
- Deploying an Azure Kubernetes Service (AKS) Cluster with Terraform: Deploy an Azure Kubernetes Service (AKS) cluster seamlessly with Terraform’s infrastructure management capabilities.
- Building an EKS Cluster on AWS with Terraform: A Step-by-Step Guide: Spin an Amazon EKS cluster effortlessly using Terraform, following our detailed step-by-step guide.
- Create a GKE Cluster on Google Cloud Platform using Terraform: Create and manage a GKE cluster on Google Cloud Platform with ease using Terraform’s automation features.
Embrace the power of Terraform and Infrastructure as Code with this comprehensive collection of articles, and enhance your skills in deploying, managing, and maintaining your infrastructure.
Subscribe to Faizan Bashir
Get the latest posts delivered right to your inbox