0
0
Terraformcloud~5 mins

Workspace naming conventions in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
When working with Terraform, you often manage different environments like development, testing, and production. Workspace naming conventions help you keep these environments organized and avoid mistakes by giving each workspace a clear, consistent name.
When you want to separate infrastructure for development and production to avoid accidental changes.
When multiple team members work on the same Terraform project but need isolated environments.
When you deploy the same infrastructure in different regions or accounts and want clear labels.
When you automate Terraform runs and need predictable workspace names for scripts.
When you want to track infrastructure changes per environment easily.
Commands
This command creates a new workspace named 'dev' for development environment to isolate its infrastructure state.
Terminal
terraform workspace new dev
Expected OutputExpected
Created and switched to workspace "dev" You are now on a new workspace called "dev".
Creates a new workspace named 'prod' for production environment to keep its state separate from other environments.
Terminal
terraform workspace new prod
Expected OutputExpected
Created and switched to workspace "prod" You are now on a new workspace called "prod".
Lists all existing workspaces so you can see which environments are available and their exact names.
Terminal
terraform workspace list
Expected OutputExpected
* dev prod
Switches to the 'dev' workspace so that Terraform commands affect the development environment.
Terminal
terraform workspace select dev
Expected OutputExpected
Switched to workspace "dev".
Key Concept

If you remember nothing else from this pattern, remember: use clear, consistent workspace names to keep your environments separate and avoid mistakes.

Common Mistakes
Using inconsistent or unclear workspace names like 'test1' or 'env2'.
It causes confusion and increases the risk of applying changes to the wrong environment.
Use descriptive names like 'dev', 'staging', 'prod' that clearly indicate the environment purpose.
Not switching to the correct workspace before running Terraform commands.
Terraform will apply changes to the wrong environment, causing potential outages or data loss.
Always run 'terraform workspace select <name>' to switch to the right workspace before applying changes.
Summary
Create workspaces with clear names to separate environments.
List workspaces to verify available environments.
Switch workspaces before running Terraform commands to target the correct environment.