run complex bash command in before_hook block terragrunt

994 Views Asked by At

Different workspaces are needed to crated and selected for different environment deployment, in bash I could do the hack

terraform workspace select $env_name || terraform workspace new $env_name

that will execute second command if first fails

My question is how can I run this in terragrunt before hooks?

#root/terragrunt.hcl

terraform {
  before_hook "workspace" {
    commands = ["plan", "apply", "destroy"]
    execute = ["terraform", "workspace", "select", local.environment]
  }

Seems terragrunt is not able to recognise the "||" sign

1

There are 1 best solutions below

0
On

I haven't been able to get it to work simply by using execute parameters, however a workaround is creating a bash file called something like workspace.sh like so:

#!/usr/bin/env bash

terraform workspace select "${1}" || terraform workspace new "${1}"

Then within the terragrunt config:

execute = [
      "bash",
      "./workspace.sh",
      get_env("WORKSPACE_NAME")
    ]