This content originally appeared on DEV Community and was authored by Haripriya Veluchamy
Hey there! ๐ Let's talk about two super useful features in Terraform: workspaces and conditional expressions. I'll explain them in the simplest way possible!
Understanding Workspaces vs Conditionals ๐ค
Let me explain this with a simple analogy:
Imagine you're building a house ๐ :
Workspaces Are Like Different Houses ๐๏ธ
- Development house (small and basic)
- Staging house (medium-sized with some features)
- Production house (large with all features)
Each house is completely separate from the others. They might look similar, but they're different buildings.
Conditionals Are Like Room Settings ๐๏ธ
Think of conditionals like light switches in a single house:
- Turn on/off features
- Adjust settings
- Enable/disable options
Let's See Some Real Examples! ๐ก
Using Workspaces
# Create different workspaces (like building different houses)
terraform workspace new dev # Small house
terraform workspace new staging # Medium house
terraform workspace new prod # Big house
# main.tf
resource "aws_instance" "server" {
# Different size server for each workspace
instance_type = lookup({
dev = "t2.micro" # Small server for dev
staging = "t2.medium" # Medium server for staging
prod = "t2.large" # Large server for prod
}, terraform.workspace, "t2.micro")
}
When to use Workspaces:
- When you need completely separate environments
- When you want to test changes safely
- When you need different sized resources for dev/staging/prod
Using Conditionals
# variables.tf
variable "enable_backups" {
type = bool
default = false
}
variable "environment" {
type = string
default = "dev"
}
# main.tf
resource "aws_instance" "server" {
instance_type = "t2.micro"
# Like switches that turn features on/off
monitoring {
enabled = var.environment == "prod" # Only on in production
}
backup {
enabled = var.enable_backups # On/off based on variable
}
}
When to use Conditionals:
- When you want to turn features on/off
- When you need to make small adjustments
- When you want to enable/disable certain settings
Real-World Example: Web Application ๐
Let's say you're building a web application. Here's how you might use both:
Using Workspaces (Different Environments)
# Each workspace gets its own setup
resource "aws_instance" "web_app" {
# Number of servers based on workspace
count = terraform.workspace == "prod" ? 3 : 1
# Server size based on workspace
instance_type = lookup({
dev = "t2.micro" # Small for development
staging = "t2.medium" # Medium for testing
prod = "t2.large" # Large for production
}, terraform.workspace, "t2.micro")
tags = {
Environment = terraform.workspace
}
}
Using Conditionals (Feature Toggles)
# In the same workspace, turning features on/off
resource "aws_instance" "web_app" {
instance_type = "t2.micro"
# Enable HTTPS only if SSL is needed
enable_https = var.ssl_required ? true : false
# Set backup retention based on importance
backup_retention = var.is_important_data ? 30 : 7
# Enable enhanced monitoring in production
monitoring {
enabled = var.environment == "prod"
}
}
Quick Summary ๐
Think of it this way:
Workspaces:
- Like having different houses
- Completely separate environments
- Different sizes and setups
- Perfect for dev/staging/prod
Conditionals:
- Like switches in one house
- Turn features on/off
- Make small adjustments
- Perfect for feature toggles
Common Use Cases ๐ฏ
Use Workspaces When:
- Setting up development environment
- Creating staging for testing
- Running production systems
- Need completely separate setups
Use Conditionals When:
- Enabling/disabling backups
- Turning on/off monitoring
- Adjusting resource sizes
- Configuring optional features
Best Practices ๐ช
-
For Workspaces:
- Keep workspace names simple (dev, staging, prod)
- Use consistent naming across projects
- Don't mix production and development resources
-
For Conditionals:
- Keep conditions simple
- Use clear variable names
- Document why you're using conditions
That's it! Now you know when to use workspaces and when to use conditionals. Remember:
- Workspaces = Different houses
- Conditionals = Switches in one house
Questions? Drop them in the comments below! ๐
This content originally appeared on DEV Community and was authored by Haripriya Veluchamy

Haripriya Veluchamy | Sciencx (2025-02-28T04:40:15+00:00) Getting Started with Terraform: Understanding Workspaces and Conditionals (Part 4) ๐. Retrieved from https://www.scien.cx/2025/02/28/getting-started-with-terraform-understanding-workspaces-and-conditionals-part-4-%f0%9f%9a%80/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.