Workspaces
Learn Terraform workspaces in detail and how you can use them in a Terraform project.
Terraform workspace
A workspace in Terraform creates many instances of Terraform code using a single project. One of the advantages of Terraform is that you can use the infrastructure as code to create your environments, reducing human error and ensuring uniformity.
You may have wondered how to create multiple environments using the same code. Everything we have done up until this point has been around creating a single instance of the infrastructure using our Terraform project, after all. If we rerun the project, it would not create another copy of the infrastructure, but would instead simply say “no changes to make”.
Workspaces are Terraform’s solution to this problem.
Project example
Let’s dive straight into an example to go into some detail about how it is working. We’ll create a new file called main.tf
:
provider "aws" {region = "us-east-2"}resource "aws_sqs_queue" "queue" {name = "${terraform.workspace}-queue"}
Most of this code should look familiar to you. The one part you probably will not have seen
before is how we are setting the queue name to "${terraform.workspace}-queue"
. The special
variable terraform.workspace
will assume the value of the current workspace we are running in.
All Terraform projects run in a workspace. Up until now, we have been running in the default
workspace. The default
workspace cannot be deleted and is the workspace you will be working in
unless you specify otherwise ...