astro icon indicating copy to clipboard operation
astro copied to clipboard

Support for workspaces?

Open GabeL7r opened this issue 7 years ago • 6 comments

Does astro support checking out a new terraform workspace to run plan?

GabeL7r avatar Jan 18 '19 22:01 GabeL7r

Astro doesn't have specific support for workspaces. Though, you could possibly run a hook to do it.

What workflow are you looking to support?

dansimau avatar Jan 19 '19 15:01 dansimau

Terraform workspaces is essentially equivalent to your use of environment but built into terraform. We use the same terraform files but checkout workspace prod or staging before running terraform apply to provision the respective environments. https://www.terraform.io/docs/state/workspaces.html

We would like to be able to use the built in workspaces instead of a separate environment variable.

Given a config block:

  - name: vpc
    path: core/vpc
    remote:
      backend_config:
        bucket: acme-terraform-states
        key: "{{.aws_region}}/vpc.tfstate"
        region: us-east-1
    workspaces:
      - prod
      - staging

Running astro plan --workspace staging would result in:

cd core/vpc -> terraform workspace select staging -> terraform plan

Running astro plan would result in:

cd core/vpc -> terraform workspace select staging -> terraform plan AND cd core/vpc -> terraform workspace select prod -> terraform plan

GabeL7r avatar Jan 22 '19 21:01 GabeL7r

OK cool, I see. In that regard, workspaces act a bit like the variables, it's just that they need a special step.

Do you know of any way to switch workspace without running terraform workspace? Is there an env var or something? Do you have to re-init after switching workspace?

dansimau avatar Jan 23 '19 08:01 dansimau

AFAIK the only way to switch workspaces is by running terraform workspace select prod, for example.

You only need to run init once and after that you can switch workspaces as often as you would like without having to re-init.

GabeL7r avatar Jan 23 '19 23:01 GabeL7r

Indeed also think the only way to switch is the workspaces command. At first init only the workspace default exists when creating workspaces the remote states are saved with the prepended with workspace key.

kevinduterne avatar Mar 08 '19 16:03 kevinduterne

It is possible to switch workspace via the TF_WORKSPACE environment variable, see the HashiCorp guide Using Terraform in automation.

marco-m-pix4d avatar Mar 10 '21 11:03 marco-m-pix4d