Terraform Workspaces

As we know, when we deploy Infrastructure, it’s prevalent that we need many and different environments, like development, testing, staging, or production.
Terraform allows us to write a scalable and sustainable infrastructure in those different environments.

The need that arises with this is, How can I reuse my code efficiently? There are many ways to accomplish this, but today we are going to focus on Terraform Workspaces.
One of the best advantages that we can achieve using Workspaces is handling the state files independently. This file represents a “photo” of our infrastructure and Terraform uses it to detect what resource will change or delete in an execution. By default, Terraform has one workspace called default. It’s probably that if you don’t
know about workspaces, you have always worked with them. When we explore this command and their sub-commands, we found:
• terraform workspace new <WORKSPACE_NAME> Create a new workspace
• terraform workspace select <WORKSPACE_NAME> Select a workspace
• terraform workspace list List available workspaces
• terraform workspace show Show the current workspace
• terraform workspace delete <WORKSPACE_NAME> Delete a workspace
States files separate
When a workspace is created and settings are applied, terraform creates a directory called terraform.tfstate.d and in it a subdirectory for each environment containing the respective tfstate file.