| by Arround The Web | No comments

Terraform Workspaces

Terraform is one of the popular IaC tools which enables us to manage and provision the cloud resources using a single configuration file. It allows us to create a reproducible infrastructure in a declarative manner which means that we define the desired state of our infrastructure, and Terraform takes care of the rest.

One feature that Terraform provides us is workspaces. This article covers the basics of Terraform workspaces and how it helps to manage multiple environments in a single configuration.

Introduction to Terraform Workspaces

Terraform workspaces provide a way to isolate our infrastructure state for different environments. Using the same Terraform code base to deploy to multiple environments without overlapping the resources becomes more manageable with the use of Terraform workspaces.

When using Terraform to provision and manage our IT infrastructure, there are scenarios where we might want to use the same configuration for different contexts so that the separate states might be necessary with the same configuration.

When having multiple accounts

If we have multiple accounts for the same cloud provider, we might use the same Terraform configuration to provision the resources in all accounts while having separate state files.

When interacting with multiple regions

We might want to use the same configuration for all regions when using Terraform to provision the resources across multiple regions within the same cloud provider.

When having multiple environments

If we have multiple environments, each may require different resources, settings, and configurations, maintaining separate Terraform configurations for each environment. It can be time-consuming and error-prone.

As the solution for the previous scenarios, we can use the Terraform workspaces which ensures that each workspace (or environment) has its dedicated state file. Thus, any changes made in one workspace will not affect the resources that are managed by another workspace.

Terraform Workspace Command

Before creating workspaces and trying things out, let’s see the options that are associated with the Terraform workspace by running the following command:

$ terraform workspace --help

 
Then, we can see all the available options to work with workspaces.


If we don’t specify a workspace, Terraform provides a default workspace called “default” which cannot be deleted. Let’s try the following command:

$ terraform workspace show
default

 
The previous command can be used to check the current workspace. Since we haven’t created any new workspaces, we can see the default workspace as the output.

Also, we can run the workspace command with the list option to see all the existing workspaces in alphabetical order.

$ terraform workspace list
* default

 

Creating a Workspace

A new workspace can be created using the “$terraform workspace new” command. Let’s make three new workspaces for development, staging, and production environments.

$ terraform workspace new dev
$ terraform workspace new staging
$ terraform workspace new production

 
The output is shown in the following:


Here, we created three new workspaces called “dev”, “staging”, and “production”, and Terraform automatically switches to the newly created workspace from the current workspace.

Now, when we run the workspace command with “show” and “list” options, we can verify that those three workspaces are created, and we are currently in the lastly designed workspace (production workspace).

$ terraform workspace show
production

$ terraform workspace list
  default
  dev
* production
  staging

 

Switch Between Terraform Workspaces

We can switch between the existing workspaces using the workspace command with the select option.

Let’s say we want to switch from the current workspace (production) to the “dev” workspace. We can use the following command:

$ terraform workspace select dev
Switched to workspace "dev".

 
Now, we successfully switched to the “dev” workspace.

Managing the Infrastructure with Terraform Workspaces

Terraform workspaces provide a feature that enables the users to handle different environments such as development, staging, and production in one configuration file. Each workspace has its state file that records the current state of the infrastructure for that environment. When the user runs the Terraform commands such as “terraform apply”, Terraform only applies the changes to the infrastructure resources that are associated with the current workspace.

Now, let’s take an example to practice what we learned.

Here, we create a simple Terraform configuration file in the “dev” environment that makes a text file called “John.txt” in the local backend including the appropriate content.

resource "local_file" "John" {
   filename = "/home/john/Desktop/John.txt"
   content = "Hello! My name is John"
}

 
Before we perform the Terraform flow (Terraform init, plan, and apply), we must verify that we are in the right environment.

$ terraform workspace show
dev

 
Now that we verified it, we can perform the Terraform flow as follows:

$ terraform init
$ terraform plan
$ terraform apply

 
We successfully created our file in the “dev” workspace. Let’s say that we need to provision the same resource in the staging environment and then change the content to “Hello world” and apply it only in the dev environment.

First, we must switch to the staging environment and perform the Terraform apply.

$ terraform workspace select staging
Switched to workspace "staging".

$ terraform apply

 
Now, we can switch back to the “dev” environment, apply the changes, and run the Terraform apply command.

$ terraform workspace select dev
Switched to workspace "dev".

$ terraform apply

 
Finally, we reached our goal.

Terraform Workspace Internals

 

When using the local state in Terraform, the state files for each workspace are stored in a directory called “terraform.tfstate.d”. Within this directory, Terraform creates a sub-directory for each workspace. Each sub-directory contains an individual state file for the specific workspace.

When using Terraform’s remote state, the workspace data is stored in the configured backend with the workspace names appended to the state path. Ensuring that the workspace names are valid for use in a URL path segment without requiring an escape to maintain the configuration integrity is crucial.

Conclusion

Terraform workspaces simplify the management of infrastructure configurations across multiple environments using a single Terraform configuration file. They keep the state of each environment separate and consistent which enables the users to manage the different environments without creating individual configurations. This tutorial taught us about Terraform workspaces and their use cases. Then, we had the basic knowledge of workspace commands and used them with examples. Finally, we looked at how to use this concept to manage multiple environments to enhance efficiency.

Share Button

Source: linuxhint.com

Leave a Reply