DEV Community

Cover image for Terraform Workspace
Surya Shankar
Surya Shankar

Posted on

Terraform Workspace

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

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

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

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

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

Image description

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

Use ssh-keygen command to create a keypair
Image description

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

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

Image description

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

terraform workspace list

Enter fullscreen mode Exit fullscreen mode

Image description

Creating the new workspace for dev and prod

terraform workspace new dev
terraform workspace new prod
Enter fullscreen mode Exit fullscreen mode

Image description

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

terraform workspace select dev
Enter fullscreen mode Exit fullscreen mode

Image description
Now we are in dev workspace , Lets run all terraform command to spin a EC2 instance

terraform init --var-file dev-terraform.tfvars
Enter fullscreen mode Exit fullscreen mode

Image description

terraform plan --var-file dev-terraform.tfvars
Enter fullscreen mode Exit fullscreen mode

Image description

terraform apply --auto-approve --var-file dev-terraform.tfvars
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

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

Image description

Image description
Image description
Image description

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

For DEV

Image description
Image description
Image description

For PROD

Image description
Image description
Image description

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.

Top comments (0)