0
0
Terraformcloud~5 mins

Terraform.workspace interpolation - Commands & Configuration

Choose your learning style9 modes available
Introduction
When managing infrastructure with Terraform, you often want to separate resources for different environments like development and production. Terraform workspaces let you do this easily by keeping different states. Using workspace interpolation helps you write flexible configurations that change based on the current workspace.
When you want to deploy the same infrastructure to multiple environments like dev, test, and prod without duplicating code.
When you need to name resources differently depending on the environment to avoid conflicts.
When you want to store environment-specific values inside your Terraform configuration automatically.
When you want to switch between environments quickly using Terraform workspaces.
When you want to keep infrastructure states isolated but use one codebase.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
}

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

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

output "bucket_name" {
  value = aws_s3_bucket.example.bucket
}

This Terraform configuration creates an AWS S3 bucket. The bucket name uses ${terraform.workspace} to include the current workspace name, so each workspace creates a uniquely named bucket. The output shows the bucket name after deployment.

Commands
Create a new workspace named 'dev' to isolate resources for the development environment.
Terminal
terraform workspace new dev
Expected OutputExpected
Created and switched to workspace "dev". You're now on a new workspace "dev".
Apply the Terraform configuration in the current workspace, creating resources with names including 'dev'.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example: Creating... aws_s3_bucket.example: Creation complete after 2s [id=my-app-dev-bucket] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: bucket_name = "my-app-dev-bucket"
-auto-approve - Skip interactive approval to apply changes immediately
Switch back to the default workspace to manage resources for the default environment.
Terminal
terraform workspace select default
Expected OutputExpected
Switched to workspace "default".
Apply the configuration in the default workspace, creating resources with names including 'default'.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example: Creating... aws_s3_bucket.example: Creation complete after 2s [id=my-app-default-bucket] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: bucket_name = "my-app-default-bucket"
-auto-approve - Skip interactive approval to apply changes immediately
Key Concept

If you remember nothing else from this pattern, remember: terraform.workspace lets you customize resource names and settings automatically based on the current workspace.

Common Mistakes
Using the same resource names without workspace interpolation.
This causes conflicts because Terraform tries to create resources with identical names in different workspaces.
Include ${terraform.workspace} in resource names to keep them unique per workspace.
Not creating or switching to a workspace before applying changes.
Terraform applies changes to the current workspace, which may not be the intended environment.
Always create and select the correct workspace using 'terraform workspace new' or 'terraform workspace select' before applying.
Summary
Create or select a Terraform workspace to isolate environment states.
Use ${terraform.workspace} in resource names to make them unique per workspace.
Apply Terraform changes in each workspace to deploy environment-specific resources.