Terragrunt: Run hooks conditionally (based on OS type, for example)

Created on 19 Apr 2018  路  3Comments  路  Source: gruntwork-io/terragrunt

Using hooks in Terragrunt gives me almost all I can imagine, but I am looking for ways to be able to run them conditionally.

There are 2 use cases:

  1. Run bash for Linux and PowerShell script for Windows (or even cygwin, which has different commands comparing to Linux)
  2. Be able to enable or disable hook if Terraform variable or environment variable is set

Any ideas or workarounds?

enhancement help wanted

Most helpful comment

Yes, a script can do that but I think this code is more general:

    after_hook "copy_main_variables_on_windows" {
      commands = ["init"]
      execute_if = "${var.os_name == "Windows"}"
      execute  = ["cp.bat", "main_variables.tf", "."]
    }

    after_hook "copy_main_variables_on_linux" {
      commands = ["init"]
      execute_if = "${var.os_name != "Windows"}"
      execute  = ["cp.sh", "main_variables.tf", "."]
    }

Also having ${get_os_name()} can be handy for this reason.

All 3 comments

Run bash for Linux and PowerShell script for Windows (or even cygwin, which has different commands comparing to Linux)

This use case makes a ton of sense, since the script itself can't do such a check, but Terragrunt could. A PR for this would be very welcome.

Be able to enable or disable hook if Terraform variable or environment variable is set

Couldn't your script check for that env var and exit accordingly?

Yes, a script can do that but I think this code is more general:

    after_hook "copy_main_variables_on_windows" {
      commands = ["init"]
      execute_if = "${var.os_name == "Windows"}"
      execute  = ["cp.bat", "main_variables.tf", "."]
    }

    after_hook "copy_main_variables_on_linux" {
      commands = ["init"]
      execute_if = "${var.os_name != "Windows"}"
      execute  = ["cp.sh", "main_variables.tf", "."]
    }

Also having ${get_os_name()} can be handy for this reason.

This is now possible using conditionals and get_platform.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mmorianos picture mmorianos  路  3Comments

goseeped picture goseeped  路  3Comments

jeffdyke picture jeffdyke  路  3Comments

jtai-omniex picture jtai-omniex  路  3Comments

shaharmor picture shaharmor  路  3Comments