Overview
Terraform workspaces provide a way to manage multiple environments using the same configuration with separate state files. This guide shows how to configure workspace-based deployments for Suga-generated stacks.Prerequisites
This guide assumed you’ve have the following:- An established multi-account AWS Architecture with appropriate IAM roles.
- A remote backend configured for Terraform state storage.
- That you’ve build your Terraform configuration using
suga build
.
Multi-Account Architecture
This workspace configuration uses a multi-account architecture with cross-account role delegation: Each target account has aTerraformRole
that trusts the administrative account, allowing Terraform to assume the role and deploy resources to the appropriate environment.
Terraform provides a great guide on getting started with this in their s3 backend configuration: https://developer.hashicorp.com/terraform/language/backend/s3#multi-account-aws-architecture
Workspace Configuration (Using AWS as an Example)
After runningsuga build
, create a providers.tf
file in your Terraform stack directory to configure workspace-based deployments:
terraform/stacks/<stack_name>/providers.tf
- Maps each workspace to a specific AWS account via IAM role delegation
- Falls back to the development account for undefined workspaces (useful for PR preview environments)
- Provides complete environment isolation at the AWS account level
Using Workspaces
Create and switch between workspaces for different environments:Workspace Commands
CI/CD Integration
Example GitHub Actions workflow for automated deployments:.github/workflows/deploy.yml
- Triggers on pushes to staging or production branches
- Automatically selects or creates the appropriate workspace
- Deploys changes to the corresponding AWS account