Terraform Workspaces provide a mechanism to manage multiple environments (e.g., development, staging, production) using the same set of configuration files. This allows you to easily switch between different states without modifying the infrastructure code. Here are some key points about Terraform Workspaces:
-
Default Workspace: When you first start using Terraform, you're in the default workspace named "default".
-
Named Workspaces: In addition to the default workspace, you can create additional named workspaces.
- Create a New Workspace:
terraform workspace new <workspace_name>
- List All Workspaces:
terraform workspace list
- Select a Workspace:
terraform workspace select <workspace_name>
- Show the Current Workspace:
terraform workspace show
- Delete a Workspace:
terraform workspace delete <workspace_name>
Consider a scenario where a GCS Bucket needs to be deployed in three different environments: dev, test, and prod. Terraform workspaces can streamline this process:
-
Initialize the new Terraform configuration by running the terraform init command in the same directory as the main.tf file:
terraform init
-
List the Workspaces in current working directory:
terraform workspace list * default
-
Create the workspaces(dev, test, prod) in the current working directory:
terraform workspace new dev terraform workspace new test terraform workspace new prod
-
List the Workspaces in current working directory :
terraform workspace list * default dev test prod
-
Select a Workspace as
dev
and it changed the workspace from default to dev workspace:terraform workspace select dev
-
See the execution plan by running the terraform plan command with var-file, var-file is the inputs of the variables:
terraform plan -var-file=_dev.tfvars -out=plan.tfplan
-
Apply you configuration now by running the command terraform apply:
terraform apply plans.tfplan
After successfully terraform apply command, it was updated the state file in the gcs bucket backend under the folder gcs-bucket-79
and created the state file asdev.tfstate
. And it was created the GCS bucket in the GCP Project.
repeated the step 4 to step 6 for the test
and prod
workspaces, only in the step 5 I changed the var-file=_test.tfvars and _prod.tfvars for the respective workspaces.
After successfully terraform apply command for the both test and prod, it was created the different GCS bucket in the GCP Project based on the .tfvars
file in both workspaces and it was updated the state file in the gcs bucket backend under the folder gcs-bucket-79
and created the state files as test.tfstate
and prod.tfstate
for the respective workspace.
Here is the list of the state files under the folder gcs-bucket-79
created by the different workspaces:
-
State Management: Each workspace has its own state file, which means changes in one workspace do not affect the state of another workspace.
-
Isolation: Workspaces are isolated from each other; resources managed in one workspace are distinct from those in another.
- Environment Management: Separate environments like dev, staging, and prod.
- Feature Branches: Isolate infrastructure changes in feature branches before merging.
- Multiple Deployments: Manage multiple deployments with a single set of configuration files.
- Naming Conventions: Use clear and consistent naming conventions for your workspaces.
- Workspace-Specific Variables: Use different variable values for different workspaces by using Terraform's
terraform.workspace
interpolation to conditionally set values. - Resource Management: Be cautious about resource names and identifiers to avoid conflicts between workspaces.
Terraform workspaces are a powerful tool for managing multiple environments within a single project. By isolating state files and maintaining consistent configurations across environments, workspaces help ensure infrastructure stability and reduce the risk of errors during deployment. Proper usage of workspaces, combined with best practices, can greatly enhance the efficiency and reliability of your infrastructure management processes.