Terraform Workspaces For Beginners

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.

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.

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
terraform workspace new staging

Selecting a Terraform Workspace

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

terraform workspace select dev



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Michael Levan

Michael Levan

Leader in Kubernetes consulting, research, and content creation ┇AWS Community Builder (Dev Tools Category)┇ HashiCorp Ambassador