cool-easter-32542
04/21/2025, 7:34 PMterraform 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 (#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 (hashicorp/terraform#21393 (comment), hashicorp/terraform#21393 (comment)), 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.
pantsbuild/pants