Stefan Xenopol
1 month
Andreas Vöst
7 months
Andreas Vöst
8 months
Emma Heinle
9 months
Andreas Vöst
9 months
Kim Klotz
10 months
Andreas Vöst
1 year

tofu/terraform workspaces with terragrunt

Posted . Visible to the public.

Warning

Workspaces Show archive.org snapshot do not work well with terragrunt generally. If at all possible try to avoid using them together.

Terragrunt configuration to use workspaces

To make workspaces work with terragrunt you need to add specific hooks to the terraform command. This will ensure terragrunt will use the correct workspace after init and before the regular commands. I was not able to get init in a workspace working.

# inside your terragrunt configuration file
terraform {
# [...] your other config

  after_hook workspace {
    commands = ["init"]
    # TG_WORKSPACE is the variable containing the workspace name, you can name it whatever you like
    execute  = ["tofu", "workspace", "select", "-or-create", get_env("TG_WORKSPACE")]
  }

  before_hook workspace {
    commands = ["plan", "state", "apply", "destroy", "refresh"]
    # TG_WORKSPACE is the variable containing the workspace name, you can name it whatever you like
    execute  = ["tofu", "workspace", "select", "-or-create", get_env("TG_WORKSPACE")]
  }
}

Issues

Only some backends support workspaces

See list Show archive.org snapshot , the http backend used with e.g. gitlab state storage does not work.

dependency outputs no longer work if dependency is also in a not-default workspace

Since the output of dependencies (probably) are in a workspace you cannot obtain it, since the init has to happen outside of the workspace.
This takes away a lot of the advantages terragrunt offers, and forces you to use data sources instead.

Sources

Ruben Aleman
Last edit
Ruben Aleman
Keywords
workspace, opentofu, terraform
License
Source code in this card is licensed under the MIT License.