Terraform Workspace - Day 5
In Terraform, workspaces provide a way to manage multiple instances of the same infrastructure within a single configuration. Workspaces allow you to create different environments, such as development, staging, and production, without duplicating the entire configuration. Each workspace has its own set of Terraform state files, allowing you to manage different environments independently.
Command -> terraform workspace
Attributes for the above command are,
new - To create a new workspace
list - To list available workspaces ('*' denotes the current workspace which you are in)
show - To show the name of the current workspace
select - To select a workspace
delete - To delete a workspace
State isolation
Each workspace has its own state file (.tfstate) that contains the information about the deployed infrastructure.
'.tfstate' file will be present under the folder, 'terraform.tfstate.d' in separate workspace environment. For default workspace '.tfstate' file will be under root folder.
This state isolation allows you to have independent Terraform state for each workspace, avoiding conflicts between environments.
Terraform commands with workspace
You need to switch workspaces using terraform workspace select <workspace_name>
before running terraform commands for a specific environment.
⏹ Create a new workspace for development
terraform workspace new dev
⏹ Switch to the development workspace
terraform workspace select dev
⏹ Apply the configuration for the development workspace
terraform apply
⏹ Switch to the production workspace
terraform workspace select prod
⏹ Apply the configuration for the production workspace
terraform apply
So basically, workspaces in Terraform provide a powerful way to manage and deploy infrastructure configurations across different environments with ease and efficiency.
Subscribe to my newsletter
Read articles from Anandhi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Anandhi
Anandhi
DevOps enthusiast on a mission to automate, collaborate, and transform software delivery