Terraform Workspace
Tags: aws, terraform, azure, devops
Published: 2022-10-09 16:17:35

**Workspaces in Terraform are simply independently managed state files. A workspace contains everything that Terraform needs to manage a given collection of infrastructure, and separate Workspaces function like completely separate working directories. We can manage multiple environments with Workspaces.**

## What is Terraform Workspace?
**If we use the local backend for storing Terraform state, Terraform creates a file called terraform.tfstate to store the state of the applied configuration. However, in scenarios where you want to use the same configuration for different contexts, separate states might be necessary with same configuration.**

**Workspaces allows you to separate your state and infrastructure without changing anything in your code when you wanted the same exact code base to deploy to multiple environments without overlap. i.e. Workspaces help to create multiple state files for set of same terraform configuration files.**

**Each of environments required separate state files to avoid collision. With the use of workspaces, you can prepend the workspace name to the path of the state file, ensuring each workspace (environment) had its own state.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t2207ekeve93vqurb4kt.png)

**To better understand how multiple Terraform workspaces work, create two different workspaces:**

**create a folder in your home directory, and change the working directory to that folder.**

**The folder is called terraform-EC2-workspace, stored in your home directory. You can name the folder differently as you prefer. This folder stores your Terraform configuration files.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10gbk5b6jpbqlfn8jx8t.png)

## Building Terraform Configurations to Launch EC2 Instances

**The provider.tf file is where you define the AWS provider so that Terraform can connect to the correct cloud services.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sdi4krhx57fbb8ljfe0q.png)

**Now, create another file named variable.tf inside the ~/terraform-EC2-workspace directory , which contains all the variables that the configuration file references.**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8qrlp98w1ehi17h3739.png)

**The terraform.tfvars contains the values Terraform uses to replace the variable references inside a configuration file.****

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thx7osz6xp7xttvsc9wq.png)

**The instance.tf and key.tf file allows you to create AWS EC2 instances with the AMI and [instance type] etc , declared as variables**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66cwuqbe3u0k6erzks1r.png)

**Use ssh-keygen command to create a keypair**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5gvb74l6m6ktk83pn1uz.png)

**Create two new files name as dev-terraform.tfvars and prod-terraform.tfvars**
**Inside dev-terraform.tfvars , change the ami to windows ami , also change to subnet id and zone**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/70dg51pm0yu4orx9jvjp.png)

**Inside prod-terraform.tfvars , change the ami to ubuntu ami , also change to subnet id and zone**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nd2mn0vrb3zizhgi0dyk.png)

**Now we are in a default workspace , to check this type :**

```
terraform workspace list

```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/txdudbl4y51htxbv6sk7.png)

**Creating the new workspace for dev and prod**

```
terraform workspace new dev
terraform workspace new prod
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ht0lojz36jz47c0wt811.png)

**Perhaps you want to switch from one workspace to another. If so, run the command below.**

```
terraform workspace select dev
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hr4bfoi1e6d359v7duf1.png)
**Now we are in dev workspace , Lets run all terraform command to spin a EC2 instance**

```
terraform init --var-file dev-terraform.tfvars
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0wfy3833opa8ghklty3.png)

```
terraform plan --var-file dev-terraform.tfvars
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5wt1vz7mcfc6a9owoj8.png)

```
terraform apply --auto-approve --var-file dev-terraform.tfvars
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2h34odzzu8z6u84u2g49.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6cm057zgkhepxl2ecpjg.png)

**Similary go for prov-terraform.tfvars after switching to it workspace**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cmvcqk9a6j2cmi1ypj14.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8oeytupbq4wckhxmeubp.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4nij80npkrlza0janyce.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkjm4r60ylh0b502dk30.png)

**It will create two terraform.tfstate file one for dev and one for prod separately as below..**

## For DEV
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jle0y5mxkwyp346m3gv3.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2y6ohdj9q97dhsl6tojr.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u0czk22tdtn6cfbr2yuz.png)

## For PROD
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yxjmly4hdh7dhesw8wcm.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8hquzrd7d3ndjl50c4e3.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1d2g004v77ebfbgmdaq.png)

## Key Points
**With Workspaces, we can set deploy different environment with same terraform configuration files.**
**To manage multiple distinct sets of infrastructure resources (e.g. multiple environments), we can use Workspaces.**
**Workspaces isolate Terraform state. It is a best practice to have separate state per environment.** 
**Workspaces are technically equivalent to renaming your state file.**
**Workspaces ensure that the environments are isolated and mirrored.**