0
0
Terraformcloud~30 mins

Creating and switching workspaces in Terraform - Try It Yourself

Choose your learning style9 modes available
Creating and switching workspaces
📖 Scenario: You are managing infrastructure using Terraform. You want to organize your infrastructure configurations by using different workspaces. Each workspace allows you to have separate states for different environments like development and production.
🎯 Goal: Create a Terraform configuration that initializes a workspace called dev, switches to it, and then creates another workspace called prod and switches to it.
📋 What You'll Learn
Create a Terraform configuration file named main.tf with a basic provider setup.
Initialize Terraform in the current directory.
Create a workspace named dev using Terraform CLI commands.
Switch to the dev workspace.
Create a workspace named prod using Terraform CLI commands.
Switch to the prod workspace.
💡 Why This Matters
🌍 Real World
Terraform workspaces are used to manage different environments such as development, testing, and production within the same configuration codebase.
💼 Career
Knowing how to create and switch Terraform workspaces is essential for infrastructure engineers and DevOps professionals to manage infrastructure lifecycle safely and efficiently.
Progress0 / 4 steps
1
Create a basic Terraform configuration
Create a file named main.tf with the following content: set the terraform block with required version >= 1.0.0 and add the provider block for hashicorp/aws with region us-east-1.
Terraform
Need a hint?

Use the terraform block to specify the required version. Use the provider block to specify AWS region.

2
Initialize Terraform and create the dev workspace
Run the Terraform initialization command terraform init in your terminal. Then create a workspace named dev using the command terraform workspace new dev.
Terraform
Need a hint?

Use terraform init to initialize. Use terraform workspace new dev to create the dev workspace.

3
Switch to the dev workspace
Switch to the workspace named dev by running the command terraform workspace select dev in your terminal.
Terraform
Need a hint?

Use terraform workspace select dev to switch to the dev workspace.

4
Create and switch to the prod workspace
Create a new workspace named prod by running terraform workspace new prod in your terminal. Then switch to the prod workspace using terraform workspace select prod.
Terraform
Need a hint?

Use terraform workspace new prod to create the prod workspace. Use terraform workspace select prod to switch to it.