Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terragrunt_validate - fails for folders without terragrunt.hcl #191

Open
mhennecke opened this issue Apr 6, 2021 · 10 comments
Open

terragrunt_validate - fails for folders without terragrunt.hcl #191

mhennecke opened this issue Apr 6, 2021 · 10 comments
Labels
estimate/1h Need 1 hour to be done feature New feature or request hook/terragrunt_validate Bash hook

Comments

@mhennecke
Copy link

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

@gchappel
Copy link
Contributor

@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!)

@github-actions
Copy link

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

@github-actions github-actions bot added the stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 10, 2021
@MaxymVlasov MaxymVlasov removed the stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 14, 2021
@MaxymVlasov
Copy link
Collaborator

terragrunt_validate hook runs only in folders with .hcl files

- id: terragrunt_validate
name: Terragrunt validate
description: Validates all Terragrunt configuration files.
entry: terragrunt_validate.sh
language: script
files: (\.hcl)$
exclude: \.terraform\/.*$

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

thetimbecker added a commit to thetimbecker/pre-commit-terraform that referenced this issue Jan 3, 2022
…tories (antonbabenko#191)

The command fails if there is no terragrunt.hcl file in the directory.
@thetimbecker
Copy link

thetimbecker commented Jan 3, 2022

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: #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.

@MaxymVlasov
Copy link
Collaborator

MaxymVlasov commented Jan 3, 2022

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

@github-actions github-actions bot added the stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 5, 2022
@MaxymVlasov MaxymVlasov removed the stale Denotes an issue or PR has remained open with no activity and has become stale. label Feb 10, 2022
@github-actions github-actions bot added the stale Denotes an issue or PR has remained open with no activity and has become stale. label Mar 13, 2022
@MaxymVlasov MaxymVlasov removed the stale Denotes an issue or PR has remained open with no activity and has become stale. label Mar 15, 2022
Repository owner deleted a comment from github-actions bot Mar 15, 2022
Repository owner deleted a comment from github-actions bot Mar 15, 2022
@github-actions
Copy link

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

@github-actions github-actions bot added the stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 15, 2022
@MaxymVlasov MaxymVlasov removed the stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 15, 2022
@mijdavis2
Copy link

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.

@MaxymVlasov
Copy link
Collaborator

@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.

@MaxymVlasov
Copy link
Collaborator

And please provide more details about your file structure and file naming, and for which propose you use .hcl files

@bkalcho
Copy link

bkalcho commented Feb 27, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
estimate/1h Need 1 hour to be done feature New feature or request hook/terragrunt_validate Bash hook
Projects
None yet
6 participants