Terraform Workspaces For Beginners

Michael Levan - Feb 16 '22 - - Dev Community

When you’re writing Terraform code, you have a state, which is in the tfstate file. The state tells the environment, whether you’re deploying to the cloud or on-prem, what it’s supposed to look like. When you, say, want to make an update to the environment, the tfstate tells the environment what’s currently there and what can be changed.

That’s great for one environment, but what about if you have multiple environments? For example; dev, staging, and production. You don’t want to always have to change the code for each environment. Instead, you need to separate the environments for dev, staging, and production. In Terraform, you do that with Workspaces.

What Are Terraform Workspaces

When you think about data, whether it’s a few files in Google Drive or some pictures, you typically separate that data. Maybe you took a vacation and you put the pictures that you took into an album on your phone. Even though the pictures are still pictures, you want them separated so you can consolidate and organize.

Terraform Workspaces aren’t much different than those pictures. When you write Terraform code, chances are you have more than one environment. You most likely have a dev environment, a staging environment, and a production environment. With all good configuration language code, the code itself shouldn’t be changing throughout each environment. The core code, for example, resources that are being created, should always be the same throughout every environment. That way, you know the code works throughout your entire Software Development Lifecycle (SDLC).

Terraform workspaces are separate pieces of Terraform state data that can be used in the same module or directory. You can use the same code for each workspace, and one workspace won’t interfere with the other.

Why Terraform Workspaces Are Useful

When you’re writing Terraform code, you’re most likely creating a module. That module can be anything from creating an S3 bucket in AWS to creating a virtual machine in Azure. The code to create those two pieces of cloud infrastructure really don’t have to change all that much. Whether you’re deploying a virtual machine in dev, staging, or production, chances are you don’t need different configurations for each environment. Why? Because the goal is always to deploy to production what was already tested in dev and staging.

The goal with Terraform workspaces is to do just that. Take the same code, deploy it to multiple environments, and not have any interferences. With Terraform workspaces, you can write one module and not have to worry about changing it for each environment.

Creating a Terraform Workspace

Now that you know why Terraform spaces are important, it’s time to learn how to create them! In this section, you’ll learn how to create a new Terraform workspace and how to switch between workspaces.

Create a Terraform Workspace

On the terminal or in VS Code, run the following command:

terraform workspace new dev
Enter fullscreen mode Exit fullscreen mode

You’ll see an output similar to the screenshot below.

Image description

Notice how it says that you are running in an empty workspace and it’s going to isolate your data. That means Terraform won’t see an existing tfstate and it’ll treat all terraform plan and terraform apply commands as if you were running in a new environment.

Try creating another workspace called staging

terraform workspace new staging
Enter fullscreen mode Exit fullscreen mode

Selecting a Terraform Workspace

Now that you’ve created two new workspaces, you can switch between them.

If you followed along in the previous section, the workspace you’re currently in should be staging. To switch to the dev workspace, run the following command:

terraform workspace select dev
Enter fullscreen mode Exit fullscreen mode

The output should be similar to the screenshot below.

Image description

Congrats! You have successfully created a new workspace and learned how to switch between workspaces.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .