0
0
Terraformcloud~30 mins

When workspaces are appropriate in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Terraform Workspaces for Environment Management
📖 Scenario: You are managing infrastructure for a web application that needs separate environments for development and production. You want to keep the infrastructure code the same but deploy it differently for each environment without mixing resources.
🎯 Goal: Learn how to use Terraform workspaces to manage multiple environments with the same configuration code.
📋 What You'll Learn
Create a Terraform configuration with a resource
Initialize Terraform workspaces for dev and prod
Use workspace-specific variables
Deploy resources separately in each workspace
💡 Why This Matters
🌍 Real World
Many companies use Terraform workspaces to manage different environments like development, staging, and production without duplicating code.
💼 Career
Understanding workspaces is essential for cloud engineers and DevOps professionals to maintain clean, scalable infrastructure deployments.
Progress0 / 4 steps
1
Create a basic Terraform configuration
Create a Terraform configuration file named main.tf with a resource aws_s3_bucket named my_bucket and set the bucket name to my-app-bucket.
Terraform
Need a hint?

Define the aws_s3_bucket resource with the exact bucket name my-app-bucket.

2
Add workspace-specific bucket name variable
Add a variable bucket_name in variables.tf and update the aws_s3_bucket.my_bucket resource to use var.bucket_name for the bucket name.
Terraform
Need a hint?

Define a variable named bucket_name and use it in the bucket resource.

3
Create workspace-specific variable files
Create two variable files: dev.tfvars with bucket_name = "my-app-bucket-dev" and prod.tfvars with bucket_name = "my-app-bucket-prod".
Terraform
Need a hint?

Create two files named dev.tfvars and prod.tfvars with the exact bucket names.

4
Initialize and use Terraform workspaces
Use Terraform CLI commands to create two workspaces named dev and prod. Then select the dev workspace and apply the configuration using dev.tfvars. Finally, select the prod workspace and apply the configuration using prod.tfvars.
Terraform
Need a hint?

Use the exact Terraform CLI commands to create and switch workspaces and apply configurations with the correct variable files.