pre-commit-terraform
pre-commit-terraform copied to clipboard
terragrunt_validate - fails for folders without terragrunt.hcl
Suppose a multi-tier project (dev,stage, prod, etc) where not every folder contains a terragrunt.hcl. Non-module folders will only contain hcl files with some "local" definitions which get inherited from the module folders, e.g.
base.hcl
- tier1
tier.hcl
module1
terragrunt.hcl
- tier2
tier.hcl
module1
terragrunt.hcl
In this setup, terragrunt_validate will fail in folders where it can't find a terragrunt.hcl.
Proposed solution:
Only run terragrunt validate
in folders, which contain a terragrunt.hcl
@antonbabenko - any chance of a review/merge with this? I'm hitting this on a regular basis which means I keep running with SKIP=terragrunt_validate
and defeating the point of having the hook. Thank you (and thank you to @mhennecke for providing a solution!)
This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days
terragrunt_validate
hook runs only in folders with .hcl
files
https://github.com/antonbabenko/pre-commit-terraform/blob/ac9299cf5747910d46edd12f311a3fd024f1cb90/.pre-commit-hooks.yaml#L80-L86
I have no setup with terragrunt right now, to check if it works or not now. @mhennecke please, confirm that issue still valid for the latest tag (now it v1.62.3) or close this issue.
repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
rev: v1.62.3
hooks:
- id: terragrunt_validate
Also, if you will use different settings, please share them here
I ran into this same problem with a similar setup. The issue is that validate
is not a first-class Terragrunt operation, so it forwards the command to Terraform. By default, when forwarding commands to Terraform, Terragrunt looks for a terragrunt.hcl
file. That is why we are running into the issue: terragrunt_validate
runs in folders with .hcl
files, but fails if there isn't also a terragrunt.hcl
file in that directory.
So either the hook needs to only run in directories with terragrunt.hcl
like @mhennecke originally said, or the .hcl
file needs to be passed as an argument to terragrunt validate
when it is run. I think the desired behavior is the former, which I included in this PR: https://github.com/antonbabenko/pre-commit-terraform/pull/312
But now I'm wondering if this hook is actually doing what it's meant to. The description says "Validates all Terragrunt configuration files.", but really it's validating Terraform through the Terragrunt wrapper. I am not sure what people are using it for, but personally I think the description should be updated.
I think, you mean terragrunt.hcl
not terraform.hcl
β¦
We can't disable possibility to check terragrunt files with different name than terragrunt.hcl
, at least because config name can be changed.
https://terragrunt.gruntwork.io/docs/reference/cli-options/#terragrunt-config
Maybe we can introduce --hook-config
as it already done for terraform_docs
to specify which files should be checked and introduce [ ! -f "$FILENAME_TO_CHECK" ] && continue
that will prevent this error
This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days
I'm seeing this as well. There are plenty of use cases where files change but terragrunt_validate should not run because there's no terragrunt.hcl but it runs anyways since there are other .hcl files.
@mijdavis2 you can redefine files: (\.hcl)$
or use exclude:
to to specify which files should be checked and which not.
As I wrote in prev message, terragrunt.hcl
is not the only valid filename for terragrunt.
And please provide more details about your file structure and file naming, and for which propose you use .hcl files
I have a similar folder structure in the live/
dir with multiple levels of terragrunt.hcl
files. The issue was that my higher level .hcl
file was not named terragrunt.hcl
, instead it was named root.hcl
. I circumvented this by renaming root.hcl
into terragrunt.hcl
and adding skip = true
to skip that file for evaluating as deployment