0
0
Terraformcloud~30 mins

Workspaces vs directory-based separation in Terraform - Hands-On Comparison

Choose your learning style9 modes available
Workspaces vs Directory-Based Separation in Terraform
📖 Scenario: You are managing infrastructure for a company that has multiple environments: development and production. You want to organize your Terraform code to handle these environments safely and efficiently.Two common ways to separate environments are using Terraform workspaces or using directory-based separation with different folders for each environment.
🎯 Goal: Build two simple Terraform configurations: one using terraform workspace to separate dev and prod environments, and another using separate directories for dev and prod. You will create a resource in each setup that shows how the environment name is used.
📋 What You'll Learn
Create a Terraform configuration with a variable environment set by workspace
Create a resource that uses the environment variable in its name
Create a separate directory structure for dev and prod with their own terraform.tfvars
Create a resource in each directory that uses the environment variable from terraform.tfvars
💡 Why This Matters
🌍 Real World
Managing multiple environments safely is common in real-world infrastructure projects. Workspaces and directory separation are two practical ways to organize Terraform code for this.
💼 Career
Cloud engineers and DevOps professionals often need to manage infrastructure for multiple environments. Knowing how to separate environments using Terraform workspaces or directories is a key skill.
Progress0 / 4 steps
1
Create Terraform configuration using workspaces
Create a Terraform file named main.tf with a variable called environment that gets its default value from the current workspace using terraform.workspace. Then create an AWS S3 bucket resource named myapp-${var.environment}-bucket using the environment variable.
Terraform
Need a hint?

Use terraform.workspace to get the current workspace name as the default value for the environment variable.

2
Add workspace creation commands and select workspace
Add a Terraform CLI command comment to create two workspaces named dev and prod. Then add a comment showing how to select the dev workspace before applying.
Terraform
Need a hint?

Use terraform workspace new to create workspaces and terraform workspace select to switch.

3
Create directory-based separation with variables and resource
Create two directories named dev and prod. In each directory, create a main.tf file with a variable environment set without a default. Then create an AWS S3 bucket resource named myapp-${var.environment}-bucket. Also create a terraform.tfvars file in each directory setting environment to the directory name.
Terraform
Need a hint?

Each directory has its own main.tf and terraform.tfvars to set the environment variable.

4
Add README comments explaining workspace vs directory separation
Add a README comment explaining that workspaces allow using one codebase with multiple environment states, while directory-based separation uses separate folders and variable files for each environment.
Terraform
Need a hint?

Explain the main difference between workspaces and directory-based separation in simple terms.