Support for Terraform workspaces with Terraform backend
Is your feature request related to a problem? Please describe.
Terraform provided the ability to segment state for different deployments in workspaces that can be created or selected with terraform workspace new <name> and terraform workspace select <name>, respectively. Using these would be easier if the TerraformDeploymentTarget accepted a workspace parameter and this was used to create or select a workspace.
Describe the solution you'd like
A workspace parameter was set, possibly by environment variable, and used like so:
terraform_deployment(
...
workspace="my-workspace",
...
)
Describe alternatives you've considered
I've tried creating a shell_command (https://github.com/pantsbuild/pants/discussions/22208) to create the workspace and set it as a dependency to a terraform_deployment but I couldn't get it to run.
I've also tried using a run_shell_command target and running pants run. This worked if there was only one run_shell_command but didn't if there were multiple, for example a monorepo with multiple terraform projects.
Additional context Terraform can sometimes create a new workspace automatically (https://github.com/hashicorp/terraform/issues/21393#issuecomment-498472958, https://github.com/hashicorp/terraform/issues/21393#issuecomment-498694545), but it is not guaranteed for all backend providers and often returns this error:
Initializing the backend...
The currently selected workspace (my-workspace) does not exist.
This is expected behavior when the selected workspace did not have an
existing non-empty state. Please enter a number to select a workspace:
...
It is more consistent to run terraform workspace new to create it.
Should Pants model Terraform workspaces as a terraform_workspace target type? (And the workspace field on terraform_deployment would then refer to the terraform_workspace target.) This would allow the Pants Terraform backend to know when it should ensure that the workspace exists first.
Either would be great, whichever is most idiomatic. A common pattern is to run terraform workspace new <workspace> || terraform workspace select <workspace> so the logic is simple and could probably in the current target as well.