Skip to main content
In this guide, you’ll learn how to create, select, and manage OpenTofu workspaces to maintain separate state files for multiple deployments of the same configuration. By the end, you’ll deploy a payroll application across three regions—US, UK, and India—using a single Terraform-compatible codebase.

Prerequisites

  • OpenTofu CLI installed and available in your PATH.
  • A sample project directory named project-sapphire.
  • Access to an S3-like backend (e.g., LocalStack) for state storage.

1. List the Default Workspace

Navigate to your project directory and list available workspaces. By default, OpenTofu starts with the default workspace.
Expected output:

2. Create New Workspaces

Isolate state per region by creating three workspaces: us-payroll, uk-payroll, and india-payroll.
Verify:

3. Select a Workspace

Switch to the us-payroll workspace before running any commands:

4. Understand Workspace State Location

OpenTofu stores each workspace’s state under terraform.tfstate.d/<workspace-name>. For example:
Do not manually edit files in the terraform.tfstate.d/ directory—always use OpenTofu commands to manage state.

5. Review the Configuration Files

Your project-sapphire folder should include:
  • variables.tf
  • provider.tf

variables.tf

Quiz:
  • Type of region? A map(string).
  • region["india-payroll"] default? "ap-south-1".
  • ami["india-payroll"] default? "ami-55140119877avm".

provider.tf


6. Update main.tf to Invoke the Module

Add a module block in main.tf that points to your shared payroll application:

7. Initialize OpenTofu

Download providers and modules:

8. Apply Configuration Across All Workspaces

Deploy the payroll app in each region:
  1. US Payroll
  2. UK Payroll
  3. India Payroll

Workspace-State Mapping


References

Watch Video

Practice Lab