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
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.
Here are some of the benefits of using Terraform workspaces:
- 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.
- 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.
- 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.
- 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.
- 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
1 |
terraform workspace new <workspace_name> |
For example, to create a new workspace called development, run the following command:
1 |
terraform workspace new development |
You can switch to the new workspace using the command
1 |
terraform workspace select <workspace_name> |
For example, to switch to the development workspace, run the following command:
1 |
terraform workspace select development |
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:
1 2 3 4 |
resource "aws_instance" "dev-server" { count = terraform.workspace == " development " ? 1 : 0 ami = "ami-0c45b159cbf" instance_type = "t2.small" |
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:
1 |
terraform plan |
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:
1 |
terraform apply |
This command will apply the changes to the infrastructure for the current workspace. You can also apply using a specific var file
1 |
terraform apply -var-file=terraform.dev.tfvars |
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
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 Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront Service Delivery Partner, Amazon OpenSearch Service Delivery Partner, AWS DMS Service Delivery Partner, AWS Systems Manager Service Delivery Partner, Amazon RDS Service Delivery Partner, AWS CloudFormation Service Delivery Partner, AWS Config, Amazon 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
Comments