pants icon indicating copy to clipboard operation
pants copied to clipboard

Support for Terraform workspaces with Terraform backend

Open d-m opened this issue 8 months ago • 2 comments

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.

d-m avatar Apr 21 '25 19:04 d-m

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.

tdyas avatar Apr 22 '25 02:04 tdyas

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.

d-m avatar Apr 22 '25 18:04 d-m