Terraform Workspace:
Workspace is a feature provided by Terraform to help you isolate your Terraform configurations and state files. It allows you to manage multiple sets of infrastructure resources independently, each with its own distinct configuration and state. This is useful when working with different environments such as development, staging, and production environments or projects. It helps you to isolate changes and prevent conflicts.
Each workspace will have a separate state file. If you don’t specify the workspace, Terraform automatically creates a default workspace named “default”.
so, let’s see the benefits of using Workspace.
1. Isolation — Workspaces enable you to manage separate environments, such as development, staging, and production without affecting one another environments.This way you can have different configuration files and state files.
2. Collaboration — Workspace gives team members a chance to work simultaneously on different environments without interfering with each other’s configurations.
3. Testing — Workspace helps the team to test configuration in dev or staging before applying in the prod environment. This reduces the risk of introducing errors
into pro.
4. Version control — It means if you want to keep different versions of configuration then we can use Workspace to keep track of versions.
Let’s see the commands to manage Workspace-
1. terraform workspace new : It will create new workspace.
e.g.- terraform workspace new prod
2. terraform workspace select : if you want to jump to an existing workspace.
e.g. — terraform workspace select prod
Note: we also have “ terraform workspace switch “ command for changing workspace. But there is a bit of a difference between “select” and “switch”.
terraform workspace select is used to set the currently active workspace without changing the state file, but terraform workspace switch is
set to the currently active workspace and changes the state file that is being used.
It means in the case of “terraform workspace select” , any changes you make to your configuration will still be applied to the state file for the
previously selected workspace. For “terraform workspace switch”, any changes you make to your configuration will be applied to the state file for newly selected workspace.
3. terraform workspace list: It will list all available workspaces.
e.g. — terraform workspace lists
output: dev
prod
staging
4. terraform workspace delete : It will delete particular Workspace.
e.g. — terraform workspace deletes dev
5. terraform workspace show: This command shows the name of the currently selected workspace. It will show the workspace in which currently you are.
e.g. — terraform workspace show
6. terraform workspace current: This command displays the current workspace’s name and path. It provides more detailed information about the currently
selected workspace.
e.g. — terraform workspace current
Let’s see some high-level commands…
7. terraform workspace lock: it locks the state for the currently selected workspace. This prevents other users from making changes to the state.
8. terraform workspace unlock: it unlocks the state for the currently selected workspace.
9. terraform workspace import it imports a workspace from a source, such as a remote Terraform Cloud workspace.
10. terraform workspace export : it exports a workspace to a destination, such as a local directory.
That’s all. Pat yourself on the back. :)
Hope you enjoyed it. Don’t forget to like it.
Top comments (0)