Introduction
When using Terraform workspaces, it is possible to accidentally delete a workspace. Without recovering the workspace, the objects in that workspace will no longer be managed by Terraform.
Scenario
When a workspace is deleted from Terraform Cloud or Enterprise, it is not possible to rollback and restore the workspace but it can be put back together using a few steps.
Recommendation
The first step to move forward is to create a fresh workspace and attach it to the Terraform remote backend.
terraform {
backend "remote" {
hostname = "app.terraform.io"
organization = "company"
workspaces {
name = "my-app-prod"
}
}
}
If the deleted workspace has no backup (IE the statefile), then a new workspace will need to be created such as the example above and terraform import is then used to import resources.
$ terraform import docker_container.web $(docker inspect --format="{{.ID}}" hashicorp-learn)
If there is a backup of the terraform state file available, the command terraform state push will load the backup state file into the new workspace.
$ terraform state push -