Using Workspaces for Environments
Explore Terraform workspaces to manage separate states for different environments (e.g., dev, staging, prod) within the same configuration.
Using Workspaces for Environments is a free DevOps Bootcamp lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What are Terraform Workspaces?
Terraform workspaces help you manage multiple, distinct states for the same Terraform configuration. Think of them as separate environments like 'dev', 'staging', or 'prod'.
This means you can use one set of .tf files to deploy infrastructure to different places without changing your code, promoting reusability and consistency.
The 'default' Workspace
When you first initialize Terraform in a directory (using terraform init), you're automatically using the default workspace.
All your initial terraform apply operations will store their state in this default workspace, unless you create and select another one. It's always present!
Creating New Workspaces
To create a new environment, use the terraform workspace new command. This command also automatically switches your current context to the newly created workspace.
Let's create a dev workspace:
terraform workspace new devTerraform will then create a separate state for this dev environment.
Listing Available Workspaces
You can see all available workspaces in your current configuration directory using the terraform workspace list command.
The currently active workspace will be marked with an asterisk (*), helping you keep track of your different environments.
terraform workspace listSwitching Workspaces
To switch between existing workspaces, use the terraform workspace select command, followed by the workspace name. This command changes your active environment.
Let's switch back to the default workspace:
terraform workspace select defaultAny subsequent Terraform operations will now target the default environment's state.
Using 'terraform.workspace'
You can make your configurations dynamic by referencing the current workspace name using the built-in terraform.workspace variable. This is perfect for naming resources or setting environment-specific tags.
Try running this example. First, run terraform init, then terraform workspace new dev, and finally terraform apply. Observe the bucket name and tags.
provider "aws" {
region = "us-east-1" # Or your preferred region
}
resource "aws_s3_bucket" "example" {
bucket = "my-unique-bucket-${terraform.workspace}-12345"
acl = "private"
tags = {
Environment = terraform.workspace
ManagedBy = "CoddyKit"
}
}
output "bucket_name" {
description = "Name of the S3 bucket created."
value = aws_s3_bucket.example.bucket
}Isolated State Files
The most critical aspect of workspaces is that each one maintains its own entirely separate state file.
This means resources deployed in your dev workspace will not interfere with those in your prod workspace, even if they're defined by the same configuration files. Terraform manages these state files in a hidden directory (.terraform/environments) within your working directory.
Deleting Workspaces
When an environment is no longer needed, you can delete its workspace using terraform workspace delete <name>.
- Important: You cannot delete the currently active workspace.
- Crucial: Always ensure you've destroyed all resources associated with that workspace using
terraform destroybefore deleting it. Otherwise, orphaned resources might remain!
terraform workspace select default
terraform workspace delete devWorkspaces vs. Separate Directories
While workspaces are great for minor variations of the same configuration, they aren't always the best choice. Consider these points:
- Workspaces: Ideal for small environmental differences (e.g., resource counts, tags) with the same core infrastructure design.
- Separate Directories: Better for significantly different infrastructure designs or when different teams manage entirely separate environments.
Choose the approach that best fits your project's complexity and team structure.
Workspace Check
You've learned about Terraform workspaces. Let's test your understanding.
Recap: Workspaces for Environments
Great job! You've explored Terraform workspaces, a powerful feature for managing multiple environments from a single configuration. Remember these key takeaways:
- Workspaces provide isolated state files for different environments (e.g., dev, prod).
- Use
terraform workspace new,list, andselectto manage them. - The
terraform.workspacevariable allows for dynamic, environment-specific configurations. - Always destroy resources before deleting a workspace to avoid orphaned infrastructure.
This allows for flexible and organized infrastructure deployment!
Frequently asked questions
Is the “Using Workspaces for Environments” lesson free?
Yes — the full text of “Using Workspaces for Environments” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Using Workspaces for Environments”?
Explore Terraform workspaces to manage separate states for different environments (e.g., dev, staging, prod) within the same configuration. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Using Workspaces for Environments” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Terraform Modules
- Creating Reusable Modules
- Using Workspaces for Environments
- Publishing Modules to a Registry