gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
7.95k stars 966 forks source link

feature request - provide terragrunt hook execution context via env variables #857

Open dlethin opened 5 years ago

dlethin commented 5 years ago

I'm in the middle of a small experimental POC with terragrunt and was interested in using a before hook as a way to execute a pre-flight script to check if certain conditions were in place before proceeding.

One condition we might want to consider is locking down to an expected version of terragrunt. We have a mechanism to pin the required version of terraform via tfenv and a .terraform-version file. However, there's no immediate obvious way to pin a required version of terragrunt.

If terragrunt could include an environment variable such as TG_VERSION to the script being executed in the hook, we could parse the value and make a decision to what exit code to use when ending the script.

There is additional context that I would imagine would be generically helpful as well:

TG_HOOK_NAME - the name specified in the terragrunt.hcl file TG_HOOK_TYPE - 'before' or 'after' TG_TF_COMMAND - 'apply', 'plan', etc -- maybe even the full command, including arguments. TG_TF_RESULT - 'pass' or 'fail'

The last few would be particularly useful in after-hook scripts that might perform some auditing or reporting/notification and need to know what the terraform command being run was and whether or not it was successful in order to tailor the underlying action.

Lastly, another thing that might be helpful to script authors is to supply the output of get_parent_terragrunt_dir(), path_relative_to_include(), and path_relative_from_include() automatically as env variables if the project specifies an include block.

While some of this information can be passed as arguments to a hook by declaring them in the hooks exec section, it makes it less cluttered in the project file for terragrunt to provide this information automatically if it can.

Thanks for sharing this tool. It's a very clever addition onto terraform.

Cheers

brikis98 commented 5 years ago

One condition we might want to consider is locking down to an expected version of terragrunt. We have a mechanism to pin the required version of terraform via tfenv and a .terraform-version file. However, there's no immediate obvious way to pin a required version of terragrunt.

See: https://github.com/cunymatthieu/tgenv

While some of this information can be passed as arguments to a hook by declaring them in the hooks exec section, it makes it less cluttered in the project file for terragrunt to provide this information automatically if it can.

Passing contextual information to hooks is a good idea. That said, I'm a little worried about using environment variables for this, as it creates a somewhat magical/invisible API.

I'd be more in favor of adding helper functions to Terragrunt that return this information so that you can explicitly pass them to your hooks as arguments. These helper functions would be likely turn out to be useful in other contexts too. PR welcome!

yi2020 commented 3 years ago

Adding to this, maybe also get_plan_file() can be added. I have an after hook that needs to know what was the name of the -out file that was used in the plan.

mwarkentin commented 10 months ago

Came across this while trying to log the terragrunt command that was run via a hook, unfortunately doesn't seem like there's a way to get the full command that was run.

Having an env var like TG_TF_COMMAND or terragrunt function to get this information would be nice.

mwarkentin commented 10 months ago

Seems like maybe this will work: https://terragrunt.gruntwork.io/docs/reference/built-in-functions/#get_terraform_cli_args