Use terraform workspace select command change currently selected workspace. a working directory, can select workspace a time. . Not Use Multiple Workspaces. Workspaces you quickly switch multiple instances a single configuration its single backend. are designed .
The terraform workspace select command used choose different workspace use further operations. Usage. Usage: terraform workspace select [DIR] command select workspace. named workspace already exist. supported flags are:-or-create - the workspace is selected not exist, create it. Default false.
To switch environments, need switch workspaces CLI commands. can all workspaces the terraform workspace list command: $ terraform workspace list default dev stage * prod. can switch a workspace the terraform workspace select command:
When run terraform plan a workspace, Terraform not access existing resources other workspaces. resources physically exist, you switch workspaces manage them. Refer the Terraform CLI workspaces documentation full details how create use workspaces. Current Workspace Interpolation
Running terraform workspace list TF_WORKSPACE set one (full workspace is prefix-one) produce following result: $ terraform workspace list 012 012-new * two Option 2: Manually populate .terraform/environment file workspace name. 1) Remove .terraform directory: `rm -rf .terraform` 2) Recreate empty .
Create new workspace terraform workspace -state=default.tfstate newspace will copy default newspace; a terraform plan check; Uncomment backend block switch in cloud run terraform init -migrate-state migrate new workspace the cloud; a terraform plan terraform workspace list .
With env0 templates, can create multiple environments the code, like switching workspaces Terraform. helps save time requiring to write duplicate code. environment have own configurations, the main code stays same all environments. Conclusion
To destroy infrastructure a specific environment, switch that workspace run: terraform workspace select dev terraform destroy. can this all environments (dev, staging .
Switch dev workspace: terraform workspace select dev. Run terraform script dev workspace: terraform apply -auto-approve. instance created be tagged dev-instance.
To create new environment called "production" switch it: terraform workspace production will establish entirely separate state the backend, terraform plan show all the resources to created fresh. can switch already-existing environments this: terraform workspace select production
Building Environments Parallelly With Terraform