Using Workspaces
Introduction
Different environments for deployments are essential for gradually rolling out changes and testing new features in one environment (e.g., testing) before applying them to another environment (e.g. production). To achieve this, you must isolate your state files for each environment. In Terraform/OpenTofu, you can do this using Workspaces or File Layouts, each with pros and cons.
Workspaces
Terraform Workspaces enable you to make multiple deployments using the same stack by isolating the state files for each deployment. This separation ensures that the state files for each deployment are stored in different locations, allowing the same infrastructure code to support multiple environments.
When using different Terraform Workspaces, you can use the target
option in Terramate to sync your deployments as distinct targets on Terramate Cloud. This feature treats each deployment with a different target as a separate entity on Terramate Cloud.
Steps
Enable the experimental target feature by setting the project config option
terramate.config.experiments= ["targets"]
before following the below steps.Create a new project, add a couple of stacks, and sync to the Terramate Cloud by following the quick-start guide up-to this step
List Available Terraform Workspaces in the bob stack:
cd stacks/bob
terraform workspace list
Sync the default workspace as the default target to Terramate Cloud:
terramate run \
--sync-drift-status \
--terraform-plan-file deploy.tfplan \
--target default \
-- \
terraform plan -out deploy.tfplan -detailed-exitcode
- Create a new workspace
prod
:
terraform workspace new prod
- Sync the Workspace as the
prd
target:
It doesn't recognize the null resource created earlier in the default workspace as the state files are isolated.
target name (
prd
) can be different from workspace name (prod
).
terramate run \
--sync-drift-status \
--terraform-plan-file plan.tfplan \
--target prd \
-- \
terraform plan -out deploy.tfplan -detailed-exitcode
- Run the
apply
command:
terramate run \
--sync-deployment \
--target prd \
-- \
terraform apply -input=false -auto-approve -lock-timeout=5m deploy.tfplan
- Verify Successful Deployment:
A successful deployment is shown in the Terramate Cloud dashboard.
Note that the target is
prd
for thebob
stack in the stacks pane.