Terraform.workspace interpolation - Time & Space Complexity
We want to understand how using Terraform's workspace interpolation affects the number of operations Terraform performs.
Specifically, how does the number of API calls or resource actions grow when using workspace interpolation in Terraform?
Analyze the time complexity of this Terraform snippet using workspace interpolation.
resource "aws_s3_bucket" "example" {
bucket = "my-bucket-${terraform.workspace}"
acl = "private"
}
output "bucket_name" {
value = aws_s3_bucket.example.bucket
}
This code creates an S3 bucket named uniquely per workspace using interpolation.
Look at what happens when we have multiple workspaces.
- Primary operation: Creating or updating one S3 bucket per workspace.
- How many times: Once per workspace, because each workspace has its own bucket.
As the number of workspaces increases, the number of buckets created or managed grows proportionally.
| Input Size (n = workspaces) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 bucket creations/updates |
| 100 | 100 bucket creations/updates |
| 1000 | 1000 bucket creations/updates |
Pattern observation: The operations grow linearly with the number of workspaces.
Time Complexity: O(n)
This means the number of operations grows directly in proportion to the number of workspaces.
[X] Wrong: "Using workspace interpolation means Terraform only creates one resource regardless of workspaces."
[OK] Correct: Each workspace is separate and manages its own resources, so the operations multiply with the number of workspaces.
Understanding how workspace interpolation affects resource creation helps you explain how Terraform manages environments separately and scales with workspace count.
"What if we used a single workspace but created multiple resources with different names instead? How would the time complexity change?"