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 a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.

CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Advanced Consulting Partner, Microsoft Gold Partner, AWS Training PartnerAWS Migration PartnerAWS Data and Analytics PartnerAWS DevOps Competency PartnerAWS GenAI Competency PartnerAmazon QuickSight Service Delivery PartnerAmazon EKS Service Delivery Partner AWS Microsoft Workload PartnersAmazon EC2 Service Delivery PartnerAmazon ECS Service Delivery PartnerAWS Glue Service Delivery PartnerAmazon Redshift Service Delivery PartnerAWS Control Tower Service Delivery PartnerAWS WAF Service Delivery PartnerAmazon CloudFront Service Delivery PartnerAmazon OpenSearch Service Delivery PartnerAWS DMS Service Delivery PartnerAWS Systems Manager Service Delivery PartnerAmazon RDS Service Delivery PartnerAWS CloudFormation Service Delivery PartnerAWS ConfigAmazon EMR and many more.

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!