Posibble to access CLI parameter values in the terragrunt.hcl?

Hi there,
I’m using terragrunt for little while but first time poster here, so bear with me if this was asked before or didn;t ask the question right.

In our project, we use --terragrunt-source in our project to provide the path to out terraform modules. We also use terraform fmt (in a script) as a before_hook. As various users setup their tf-modules directory differently, as per their liking, I found it’s next to impossible to writing the script (executes, something like: terraform fmt ${tf_module} in it), which works for the team. I was wondering, if there any way the commnd-line parameter values can be imported as ENV_VARIABLE or something so that it’s accessible in the terragrunt.hcl, so that I can use that as a parameter to supply to the script? Or any other way to achieve the similar thing?

Any help or pointing me t the right direction would be greatly appreciated!!

-S

Any one with any thought(s) at all?
All I need is to be able to capture the Terraform module-path in a variable (or something like that) so that I can feed that info to the script that runs as before_hook - any pointer(s)?

-S

Hi @dsantanu ,

Hooks run from the directory with the terraform module, except for hooks related to terragrunt-read-config and init-from-module . These hooks run in the terragrunt configuration directory (the directory where terragrunt.hcl lives). Given that, setting the hook to terraform fmt (no arg) should have the desired effect.

Hope this helps!
- Yori

Hi @yoriy ,Thanks for the reply.
I tried that but unfortunately that doesn’t actually work. The Terragrunt documentation on before_hook is confusing, IMO - the hook doesn’t run from the actual terraform module directory but in/from the cached terragrunt directory, like: .terragrunt-cache/ZcYtXnHH8M36Mz-ntqxwr-UxFQY/dCWM_rlVNlHFMGswUwt4pOE2BH4, hence terraform fmt fixes stuff in that temp location but not the actual code base, which makes the whole purpose useless.
Or, am I missing something here?

-S

So, does it mean there is no way to get the actual/physical location of the tf-modules in HCL if the module-path is supplied using --terragrunt-source?

Hi sorry for the delay in responding here.

So, does it mean there is no way to get the actual/physical location of the tf-modules in HCL if the module-path is supplied using --terragrunt-source ?

Yes that is correct. That said, I think we might be able to implement a helper function that flows this through to the config when set. I filed Add helper function to get `--terragrunt-source` · Issue #1555 · gruntwork-io/terragrunt · GitHub so you can track the feature request.

thanks @yoriy !!
It will really useful to run e.g. terraform fmt on the actual location as the post-hook - I’m stuck at there.

you could pass the directory you want to run terraform fmt in as an env variable, then get the value using get_env as an input to a script you run with an after_hook, like:

locals {
    fmt_location = get_env("FMT_LOCATION")
}
terraform {
    before_hook "validate_one_region_per_account" {
        commands = ["plan", "apply"]
        execute      = ["cd ${local.fmt_location} ; terraform fmt"]
}

however, it doesn’t really make sense to do this in terraform at all if the code you’re formatting is being tracked in git because you’ll end up with uncommitted changes after any terraform run - that’s more trouble than it’s worth. instead, you should make terraform fmt run before your changes are committed; write a git precommit hook instead. there are already examples for how to do this online; check out Pre-Commit Hooks for Terraform. Enforcing Terraform Linting and… | by Frank Kerschbaumer | Slalom Build | Medium or maybe something simpler like A Terraform validation and formatting pre-commit hook · GitHub if you just want formatting