Terraform workspaces

What are Terraform Workspaces and How Can You Use Them?

Companies across the globe are struggling to become data-driven. 72% surveyed reported that they had not created a data culture or environment.

Proactive business relies on data to ensure that all operations are functional and maintain the highest productivity level.

Are you interested in optimizing your workloads? Do you want to move your infrastructure to the cloud for better management?

If so, keep reading to learn more about Terraform workspaces.

What Is Terraform?

Terraform is an infrastructure as code (IaC) software tool that enables cloud infrastructure engineers to create and build on existing infrastructure.

It relies on declarative syntax in configuration files that determine what the infrastructure's final state should be when run.

For example, a simple configuration for an AWS S3 bucket might look like this:


provider "aws" {
  region = "ca-central-1"
}

resource "aws_s3_bucket" "private" {
  bucket = "my-private-bucket"
  acl    = "private"

  tags = {
    Name        = "My private bucket"
    Environment = "Staging"
  }
}

Executing this can be as simple as:

$ terraform apply

This command will cause Terraform to formulate a "plan" based on your infrastructure's current state. However, you must already have AWS credentials configured to enable CLI access to your AWS account. It will then prompt you to confirm the changes and execute them.

In this simple example we use a local "backend" to store state files. These state files enable Terraform to distinguish between the existing and desired state of your infrastructure. A local backend works well for a single developer, but for teams, you will need to use a remote state to enable team members to share state files and avoid unexpected changes.

Terraform supports a vast number of AWS resources. By tracking your templates in a source control system like Git, you will enjoy high visibility and the ability to quickly restore your infrastructure to the desired state by reapplying your templates.

Terraform is compatible with several providers such as AWS and Microsoft Azure, Google Cloud, Oracle Cloud, Kubernetes, Alibaba and others. It can be used even for on-premises infrastructure, and you can mix providers within the same project.

Terraform uses a simple syntax and is safe to use for provisioning production-grade IT infrastructure. If you're interested in a hybrid-cloud or multi-cloud infrastructure, you will likely need to use Terraform to provision this properly.

What Is a Terraform Workspace?

Workspaces each have state files, and as such, they provide isolation between them. When working in one workspace, changes will not affect resources in another workspace. This separation is critical for peace of mind when managing large deployments.

In the earlier example of creating an S3 bucket, we used the default workspace, which requires no configuration. To create a new workspace, it's as simples as:

$ terraform workspace new dev

If you were using multiple workspaces, say for different environments like development, staging, and production, you could change the S3 bucket configuration to use variable scoping for resource names using the terraform.workspace variable, like so:


resource "aws_s3_bucket" "private" {
  bucket = "my-private-bucket-${terraform.workspace}"
  acl    = "private"

  tags = {
    Name        = "My private bucket"
    Environment = terraform.workspace
  }
}

You'll notice that above we also changed the hard-coded environment value to use the workspace variable.

Additional Terraform Workspace Commands

To list all your existing workspaces, do the following:

$ terraform workspace list

If we want to switch back to the default workspace:

$ terraform workspace select default

And to verify the change:

$ terraform workspace show

Workspaces and IaC in general with Terraform can be very useful for infrastructure management and automation.

How Can IaC Benefit Your Projects?

IaC lets developers codify their infrastructure, making tasks like provisioning new resources or making changes quicker, more reliable, and even automatic.

Since Terraform is an IaC tool, it's an excellent option for reaping the benefits that come with infrastructure-as-code, such as:

Greater Speed

IaC enables your team to push infrastructure changes faster and more frequently, so you don't have to worry about manually working your way through an interface. You can instead focus your time on development.

By integrating your source control system with an automated deployment pipeline, your infrastructure changes can be deployed even faster by having them deploy automatically when changes to your code are pushed.

More Reliability

You may have an extensive infrastructure with a lot of provisioning or changes going on at one time. There is a risk of accidentally deviating from the desired state with manual configuration.

When you use an IaC like Terraform, your resources will be deployed as defined from the start. When Terraform runs, it evaluates the proposed changes and outputs errors if the configuration is broken, which improves developer efficiency.

Facilitated Learning

When making manual configuration changes in the AWS console, it's sometimes not easy to wrap one's head around how everything fits together and what all the options are. For example, with Terraform and its robust but concise documentation, developers can more quickly understand the resources, configure them, and understand their dependencies and dependants. This clarity is fantastic for accelerating learning.

Easier Testing and Optimization

Since IaC makes everything faster and more dependable, you can also use it to test changes or experiment a little bit without worry.

You don't even need to use a lot of resources or time to do this. For this reason, it is a great way to test potential improvements or amendments before implementing them.

When you test for maximum optimization levels and find something that works, IaC makes it easy to scale up to that new level in your production environment.

Configuration Drift Prevention

Configuration drift happens if the original configuration no longer matches what exists in the infrastructure's environment.

The back-up is not the same as the current state, so a data centre would not recover its current state if needed. Configuration drift is a nightmare, especially if you have a large and intricate infrastructure.

IaC helps you adhere to a specific plan from the start, and since it makes it so easy to define changes and reapply the desired state as you go and learn, this threat is significantly diminished.

Local Terraform vs Terraform Cloud

Terraform Cloud is a managed service from HashiCorp, the makers of Terraform. It is a remote backend for your state files and enables automated provisioning of infrastructure based on events, similar to a CI/CD system. It can integrate with your version control system.

A local workflow with Terraform manages a specific collection of infrastructure within a single working directory. This directory-based isolation means that configurations within each directory are isolated from the others. Engineers can organize resources into groupings as they see fit.

Terraform Cloud organizes configurations using workspaces rather than directories. The workspaces each manage the metadata needed to control the infrastructure they configure. Terraform Cloud itself uses remote execution to manage your infrastructure using virtual machines. This remote execution comes with great benefits for teams.

Why Use Terraform Workspaces in Terraform Cloud?

Terraform run locally is simple to set up, fast, and ideal for individual developers. However, with Terraform Cloud, your infrastructure management is centralized and independent of developer workstations. This centralization is useful for productivity and helps to promote parallel development across the board.

You can assign different roles to different responsibilities. This configurability of permissions can be done within the workspaces to manage the infrastructure as a whole. Each role can have unique permissions and access to tools, simplifying the process of delegating tasks.

Terraform Cloud transforms workspaces into shared resources to which you can control access and monitor. It makes it easier to automate practices and resources within the infrastructure, also making it easier to change things as needed and with enhanced visibility.

Hire a Professional for Your Project

Cloud infrastructure and architecture can be complicated, but we are here to get you up and running and learn what you need to be successful.

Are you looking for someone to help you move your work to Terraform?

If you're interested in learning more about how we can help you implement Terraform workspaces, contact us today for a quote.