0
0
Terraformcloud~5 mins

Workspaces vs directory-based separation in Terraform - CLI Comparison

Choose your learning style9 modes available
Introduction
When managing infrastructure with Terraform, you often need to separate environments like development and production. You can do this by using Terraform workspaces or by keeping separate directories for each environment. Each method helps keep your infrastructure organized and prevents changes in one environment from affecting another.
When you want to manage multiple environments like dev, staging, and prod using the same Terraform configuration files.
When you want to avoid duplicating code but still keep environment states separate.
When you prefer a simple folder structure to isolate environments completely.
When you want to switch environments quickly without changing directories.
When you want to keep environment configurations fully independent for safety.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.3.0"
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example" {
  bucket = "example-bucket-${terraform.workspace}"
  acl    = "private"
}

This Terraform configuration creates an AWS S3 bucket. The bucket name includes the current workspace name, so each workspace creates a separate bucket.

The terraform block sets the required Terraform version.

The provider block configures AWS region.

The resource block defines the S3 bucket with a name that changes based on the workspace.

Commands
Initializes the Terraform working directory, downloads provider plugins, and prepares the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work.
Lists all existing Terraform workspaces to see which environments are available.
Terminal
terraform workspace list
Expected OutputExpected
* default
Creates a new workspace named 'dev' to separate the development environment state.
Terminal
terraform workspace new dev
Expected OutputExpected
Created and switched to workspace "dev". You're now on a new workspace called "dev". If you ever want to switch back to the default workspace, run terraform workspace select default
Applies the Terraform configuration in the current workspace, creating resources specific to that workspace.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example: Creating... aws_s3_bucket.example: Creation complete after 2s [id=example-bucket-dev] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips interactive approval before applying changes
Switches back to the default workspace to manage or create resources for another environment.
Terminal
terraform workspace select default
Expected OutputExpected
Switched to workspace "default".
Key Concept

If you remember nothing else from this pattern, remember: Terraform workspaces let you use the same code to manage multiple environments by keeping their states separate, while directory-based separation uses different folders to isolate environments completely.

Common Mistakes
Using the same workspace for multiple environments without switching workspaces.
This causes Terraform to overwrite or mix states, leading to resource conflicts or accidental changes across environments.
Always create and switch to a separate workspace for each environment before applying changes.
Mixing workspace usage with directory-based separation without clear boundaries.
This can cause confusion about which state is active and where resources are managed, increasing risk of errors.
Choose one method clearly: either use workspaces with a single directory or separate directories with their own state files.
Hardcoding resource names without including workspace or environment identifiers.
Resources may clash or overwrite each other when using multiple environments in the same account.
Include the workspace name or environment identifier in resource names to keep them unique.
Summary
Initialize Terraform with 'terraform init' to prepare the working directory.
Use 'terraform workspace new <name>' to create separate environments sharing the same code.
Apply changes in the current workspace with 'terraform apply' to keep states isolated.
Switch workspaces with 'terraform workspace select <name>' to manage different environments.
Include workspace names in resource identifiers to avoid conflicts.