# Terraform Workspaces with a simple example use case.

# What is a terraform workspace?

A TF workspace isolates state information within a workspace. At the time of workspace creation, an isolated TF backend is created. This ensures existing configurations are not disturbed.

### Sounds interesting? Let us find some interesting use in this blog post.

> `Using terraform workspaces is equivalent to using a container with different volumes.`

<mark>NVM :p that's a metaphor.</mark>

By default, every time you do terraform init a workspace is created which is a default workspace. So by design or by choice, we are using terraform workspace every day.

*you can quickly switch between workspaces using the terraform workspace cli. (Refer to TF workspace* *cheat* *sheet below)*

### *Definition as in documentation:*

`You can create multiple working directories to maintain multiple instances of a configuration with completely separate state data.`

As defined, state data if isolated between workspaces which helps to use the same code multiple provisioning.

### To understand better let us try an example use case:

*An identical infrastructure should be provisioned in two different regions, us-east-1(N.Virginia), and eu-west-1(Ireland)*

Provided the use case, there are several ways to achieve this, however, let us see an example with terraform workspaces.

I have used a local provisioner for this demo to keep it simple. I did not create any workspace yet, let us check which workspace we are in.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674543302163/b611fe5a-5ff7-4855-baef-47e7635dbc67.png align="center")

Below terraform creates a new txt file with text inside "Workspace Blog post". Also if you notice I have used terraform workspace variable to dynamically name the txt file.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674543479263/f5455465-902f-463e-a77d-e1f8c3ce89fc.png align="center")

This will help us to identify from which workspace execution this file was created. Let us plan and apply our script. And a file will be created as shown below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674543599853/04adbfd6-0dc1-47c8-b20b-e3a6e9ccf072.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674543645477/98edef2e-e976-4d8c-a206-9dc68b5f9c69.png align="center")

let us create a new workspace with the same blog-demo, and make sure we are in the correct workspace.

`terraform workspace new blog-demo`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674543690703/01ef5ae7-b239-449d-878b-863e9169a0e2.png align="center")

Successfully created and terraform workspace list will display all the workspaces available. Trying to terraform plan and apply will results as shown below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674543743769/b4b7fae0-48f8-4adc-9cc8-8c123cbf7375.png align="center")

With the same terraform code without any modifications we had two different executions.

Now co-relate this with provisioning multi-region infrastructure with the common code base.

### Key takeaways of using terraform workspace.

* in a similar approach, we can differentiate between regions based on the workspace names.
    
* terraform execution is faster since it will reuse the modules and packages downloaded stead of downloading again.
    
* increases code reusability and efficiency.
    

And any more use cases, it grows depending on our design pattern.

## Workspace commands cheat sheet.

*To list all the existing workspaces.*

```bash
 terraform workspace list
```

*To select/switch to a new workspace:*

```bash
terraform workspace select <workspace name>
```

*To create a new workspace:*

```bash
terraform workspace new <workspace name>
```

*To delete a workspace*

```bash
terraform workspace delete  <workspace name>
```

ignore dependencies and track

```bash
terraform workspace delete -force <workspace name>
```

*To display the current active workspace:*

```bash
terraform workspace show
```

Thank you, Peace!

**References:**

Feel free to go through these articles if you prefer a more detailed understanding.

[https://spacelift.io/blog/terraform-workspaces](https://spacelift.io/blog/terraform-workspaces)

[https://developer.hashicorp.com/terraform/language/state/workspaces](https://developer.hashicorp.com/terraform/language/state/workspaces)

[https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file)
