Workspace

The terraform workspace command is used to manage workspaces.

List

terraform workspace list command is used to list the workspaces. by default, default workspace is created.The current workspace is indicated using an asterisk (*) marker.

┌──(gaurav㉿learning-ocean)-[~/terraform/youtube-course/terraform-workspace]
└─$ terraform workspace list
* default

Create

The terraform workspace new command is used to create a new workspace.

This command will create a new workspace with the given name. A workspace with this name must not already exist

terraform workspace new dev

let's create a new workspace using below command

┌──(gaurav㉿learning-ocean)-[~/terraform/youtube-course/terraform-workspace]
└─$ terraform workspace new test
Created and switched to workspace "test"!
You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.

list all the created workspace

┌──(gaurav㉿learning-ocean)-[~/terraform/youtube-course/terraform-workspace]
└─$ terraform workspace list
  default
* test

we can see that test workspace created and selected.

Show

terraform workspace show command will display the current workspace.

┌──(gaurav㉿learning-ocean)-[~/terraform/youtube-course/terraform-workspace]
└─$ terraform workspace show
test

so right now you are in test workspace and if you run terraform apply then tf.state will be create in that workspace.

Switch Workspace

If you want to switch workspace then you can use terraform workspace select command.

example:

┌──(gaurav㉿learning-ocean)-[~/terraform/youtube-course/terraform-workspace]
└─$ terraform workspace list
  default
* test
┌──(gaurav㉿learning-ocean)-[~/terraform/youtube-course/terraform-workspace]
└─$ terraform workspace select default
Switched to workspace "default".
┌──(gaurav㉿learning-ocean)-[~/terraform/youtube-course/terraform-workspace]
└─$ terraform workspace list
* default
  test

Delete Workspace

If you want to delete any existing workspace then you can use terraform delete command.

┌──(gaurav㉿learning-ocean)-[~/terraform/youtube-course/terraform-workspace]
└─$ terraform workspace delete test
Deleted workspace "test"!

Demo Video