0
0
Terraformcloud~30 mins

Default workspace in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Terraform Default Workspace Setup
📖 Scenario: You are setting up a Terraform project to manage cloud infrastructure. Terraform uses workspaces to separate different environments like development and production. By default, Terraform uses the default workspace if no other workspace is selected.In this project, you will create a Terraform configuration that uses the default workspace to deploy a simple resource.
🎯 Goal: Create a Terraform configuration that initializes the default workspace and deploys a simple resource using it.
📋 What You'll Learn
Create a Terraform configuration file named main.tf.
Use the default workspace without creating or switching to any other workspace.
Define a simple resource (e.g., a null resource) to confirm the default workspace is used.
💡 Why This Matters
🌍 Real World
Terraform workspaces help manage multiple environments like development, staging, and production using the same configuration code but separate states.
💼 Career
Understanding Terraform workspaces is essential for cloud engineers and DevOps professionals to manage infrastructure environments safely and efficiently.
Progress0 / 4 steps
1
Create a Terraform configuration file with provider setup
Create a file named main.tf and add a terraform block with required_version set to ">= 1.0.0". Then add a provider block for null provider with no configuration.
Terraform
Need a hint?

The terraform block sets the Terraform version requirement. The null provider is used for testing and does not require configuration.

2
Add a null_resource to the configuration
In the main.tf file, add a resource block for null_resource named example with no additional arguments.
Terraform
Need a hint?

The null_resource is a simple resource that does nothing but can be used to test Terraform configurations.

3
Initialize Terraform to use the default workspace
Run terraform init to initialize the working directory. Then ensure the configuration uses the default workspace by not creating or switching to any other workspace in the configuration.
Terraform
Need a hint?

Terraform uses the default workspace automatically if you do not specify any workspace configuration.

4
Add an output to confirm the default workspace is active
Add an output block named current_workspace that uses the built-in Terraform function terraform.workspace to output the name of the current workspace.
Terraform
Need a hint?

The terraform.workspace function returns the name of the workspace Terraform is currently using.