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 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
Click to Comment