Workspaces vs directory-based separation in Terraform - Performance Comparison
We want to understand how the time to apply Terraform changes grows when using workspaces versus separate directories.
Which method scales better as the number of environments increases?
Analyze the time complexity of managing multiple environments using workspaces or directories.
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "env/${terraform.workspace}/state.tfstate"
region = "us-west-2"
}
}
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-123456"
instance_type = "t2.micro"
}
This code uses workspaces to separate environment states while provisioning multiple instances.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Terraform plan and apply for each environment workspace or directory.
- How many times: Once per environment, repeating for each workspace or directory.
As the number of environments grows, you run Terraform commands separately for each workspace or directory.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 10 separate Terraform applies |
| 100 | About 100 separate Terraform applies |
| 1000 | About 1000 separate Terraform applies |
Pattern observation: The total operations grow roughly in direct proportion to the number of environments.
Time Complexity: O(n)
This means the time to manage all environments grows linearly as you add more environments.
[X] Wrong: "Using workspaces means Terraform applies all environments at once, so time stays the same."
[OK] Correct: Each workspace is managed separately, so you still run Terraform commands per environment, making time grow with the number of environments.
Understanding how Terraform scales with environment separation helps you design infrastructure management that stays efficient as projects grow.
"What if we automated running Terraform apply for all environments in parallel? How would that affect the time complexity?"