0
0
Terraformcloud~30 mins

Terraform.workspace interpolation - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Terraform.workspace Interpolation
📖 Scenario: You are managing infrastructure for multiple environments like development and production. You want to use Terraform to create resources that change based on the current workspace.
🎯 Goal: Build a Terraform configuration that uses terraform.workspace interpolation to set resource names dynamically depending on the active workspace.
📋 What You'll Learn
Create a Terraform variable to hold a base resource name
Define a local value that combines the base name with the current workspace using terraform.workspace
Create an AWS S3 bucket resource using the combined name
Add a tag to the bucket that shows the current workspace
💡 Why This Matters
🌍 Real World
Terraform workspaces help manage multiple environments like dev, test, and prod using the same configuration but different resource names.
💼 Career
Cloud engineers use <code>terraform.workspace</code> interpolation to write reusable infrastructure code that adapts to different deployment environments.
Progress0 / 4 steps
1
Create a Terraform variable for the base resource name
Create a Terraform variable called base_name with the default value myapp.
Terraform
Need a hint?

Use the variable block with default set to "myapp".

2
Define a local value combining base_name and terraform.workspace
Add a locals block with a local value called bucket_name that combines var.base_name and terraform.workspace separated by a dash.
Terraform
Need a hint?

Use locals block and string interpolation with var.base_name and terraform.workspace.

3
Create an AWS S3 bucket resource using the local bucket_name
Create a resource block aws_s3_bucket named example with the bucket name set to local.bucket_name.
Terraform
Need a hint?

Use resource "aws_s3_bucket" "example" and set bucket = local.bucket_name.

4
Add a tag to the bucket showing the current workspace
Inside the aws_s3_bucket.example resource, add a tags block with a tag Environment set to terraform.workspace.
Terraform
Need a hint?

Add a tags block with Environment = terraform.workspace inside the resource.