Cloud Computing, DevOps

3 Mins Read

Terraform Workspaces to Manage Multiple Environments

Voiced by Amazon Polly

Overview

Terraform is an infrastructure as a code tool that allows users to create, manage, and provision cloud infrastructure resources. One of the useful features of Terraform is its workspace functionality. Workspaces enable users to manage multiple infrastructure resources within a single configuration file.

Pioneers in Cloud Consulting & Migration Services

  • Reduced infrastructural costs
  • Accelerated application deployment
Get Started

What is a Terraform workspace?

A Terraform workspace is a way to create multiple instances of the same infrastructure in different environments. For example, you may have a development environment, a staging environment, and a production environment. Each environment may require the same infrastructure but with different configurations.

A Terraform workspace is essentially a distinct environment for your infrastructure resources. Each workspace can have its own set of variables, resources, and state files. By using workspaces, you can manage and deploy different sets of resources in parallel without interfering with each other.

Here are some of the benefits of using Terraform workspaces:

  1. Organize resources

Workspaces allow you to organize your infrastructure resources into logical groups. For example, you can create separate workspaces for development, testing, and production environments. This helps to keep your resources organized and makes it easier to manage them.

  1. Reduce errors

With Terraform workspaces, you can avoid errors caused by sharing resources across different environments. For example, if you accidentally delete a resource in one workspace, it won’t affect resources in other workspaces.

  1. Share infrastructure code

You can share your infrastructure code across different environments, reducing the need to maintain a separate set of code for each environment.

  1. Improve collaboration

Workspaces can be used to collaborate with other members of your team. Each team member can work on their workspace without interfering with the work of others.

  1. Flexibility

Using workspaces allows you to create and manage multiple environments with the same infrastructure codebase. You can easily create new environments or delete existing ones as needed.

How to create a Workspace in Terraform?

To create a new workspace in Terraform, use the command

For example, to create a new workspace called development, run the following command:

You can switch to the new workspace using the command

For example, to switch to the development workspace, run the following command:

When you switch to a new workspace, Terraform creates a new state file to store the state of the resources for that workspace.

Managing Workspace-Specific Resources

To manage workspace-specific resources in Terraform, you can use terraform.workspace variable. This variable contains the name of the current workspace and can be used in your Terraform code to create or delete resources based on the workspace conditionally.

For example, you can create a conditional block that creates a resource only in the development workspace:

In this example, the aws_instance resource is created only if the current workspace is in development. If the current workspace is not developed, the resource is not created.

Using Terraform workspaces

Once you have created a workspace, you can use it to manage your infrastructure in that environment. For example, you can use the terraform plan command to see what changes will be made to the infrastructure for this workspace:

This command will show you the changes that will be made to the infrastructure for the current workspace.

You can then use the terraform apply command to apply the changes:

This command will apply the changes to the infrastructure for the current workspace. You can also apply using a specific var file

Conclusion

Terraform workspaces are a powerful tool for managing multiple infrastructure resources within a single configuration file. You can organize resources, reduce errors, share infrastructure code, and improve collaboration using workspaces.

Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.

  • Cloud Training
  • Customized Training
  • Experiential Learning
Read More

About CloudThat

CloudThat is also the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Drop a query if you have any questions regarding Terraform, Workspaces and I will get back to you quickly.

To get started, go through our Consultancy page and Managed Services Package that is CloudThat’s offerings.

FAQs

1. How can I see all the workspaces created?

ANS: – The command terraform workspace list will list all existing workspaces. The current workspace is indicated using the sign (*).

2. Can I create separate state files for each workspace?

ANS: – Yes, when you create a new workspace, Terraform automatically creates a separate state file for that workspace. This means that each workspace has its state, making it possible to manage and deploy multiple environments with different configurations from a single codebase without interfering with each other.

WRITTEN BY Rekha S

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!