Terraform Workspaces with a simple example use case.
What is a terraform workspace? And how to use it?
What is a terraform workspace?
A TF workspace isolates state information within a workspace. At the time of workspace creation, an isolated TF backend is created. This ensures existing configurations are not disturbed.
Sounds interesting? Let us find some interesting use in this blog post.
Using terraform workspaces is equivalent to using a container with different volumes.
NVM :p that's a metaphor.
By default, every time you do terraform init a workspace is created which is a default workspace. So by design or by choice, we are using terraform workspace every day.
you can quickly switch between workspaces using the terraform workspace cli. (Refer to TF workspace cheat sheet below)
Definition as in documentation:
You can create multiple working directories to maintain multiple instances of a configuration with completely separate state data.
As defined, state data if isolated between workspaces which helps to use the same code multiple provisioning.
To understand better let us try an example use case:
An identical infrastructure should be provisioned in two different regions, us-east-1(N.Virginia), and eu-west-1(Ireland)
Provided the use case, there are several ways to achieve this, however, let us see an example with terraform workspaces.
I have used a local provisioner for this demo to keep it simple. I did not create any workspace yet, let us check which workspace we are in.
Below terraform creates a new txt file with text inside "Workspace Blog post". Also if you notice I have used terraform workspace variable to dynamically name the txt file.
This will help us to identify from which workspace execution this file was created. Let us plan and apply our script. And a file will be created as shown below.
let us create a new workspace with the same blog-demo, and make sure we are in the correct workspace.
terraform workspace new blog-demo
Successfully created and terraform workspace list will display all the workspaces available. Trying to terraform plan and apply will results as shown below.
With the same terraform code without any modifications we had two different executions.
Now co-relate this with provisioning multi-region infrastructure with the common code base.
Key takeaways of using terraform workspace.
in a similar approach, we can differentiate between regions based on the workspace names.
terraform execution is faster since it will reuse the modules and packages downloaded stead of downloading again.
increases code reusability and efficiency.
And any more use cases, it grows depending on our design pattern.
Workspace commands cheat sheet.
To list all the existing workspaces.
terraform workspace list
To select/switch to a new workspace:
terraform workspace select <workspace name>
To create a new workspace:
terraform workspace new <workspace name>
To delete a workspace
terraform workspace delete <workspace name>
ignore dependencies and track
terraform workspace delete -force <workspace name>
To display the current active workspace:
terraform workspace show
Thank you, Peace!
References:
Feel free to go through these articles if you prefer a more detailed understanding.
https://spacelift.io/blog/terraform-workspaces
https://developer.hashicorp.com/terraform/language/state/workspaces
https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file