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

feat: Add terragrunt_validate_inputs hook to check unused and undefined inputs #677

Merged
merged 10 commits into from
Jun 19, 2024

Conversation

jmreicha
Copy link
Contributor

@jmreicha jmreicha commented Jun 8, 2024

Put an x into the box if that apply:

  • This PR introduces breaking change.
  • This PR fixes a bug.
  • This PR adds new functionality.
  • This PR enhances existing functionality.

Description of your changes

Adds the ability to define a new terragrunt_validate_inputs hook for checking for unused and undefined variables in terragrunt.hcl configuration files.

Note: This does seem to require auth. I haven't found a way to get the hook to work without first authenticating to the cloud provider I was testing with. Please let me know if there is a way around this.

Fixes #675

How can we test changes

Add the new pre-commit config:

- repo: https://github.com/jmreicha/pre-commit-terraform
    rev: tg-validate
    hooks:
      - id: terragrunt_validate_inputs
        args:
          - --args=--terragrunt-strict-validate

Intentionally introduce a misconfiguration in a terragrunt.hcl configuration.

inputs = {
	...
	foo = "bar"
}

Run the pre-comit hook and note the failure.

Terragrunt validate inputs...............................................Failed
- hook id: terragrunt_validate_inputs
- exit code: 1

WARN[0030] The following inputs passed in by terragrunt are unused:  prefix=[/home/jmreicha/github.com/lytx/cloudops-terragrunt/dataplatform/stage/us-west-2/redshift/ore-dp-non-prod-redshift-cluster]
WARN[0030]      - foo                                        prefix=[/home/jmreicha/github.com/lytx/cloudops-terragrunt/dataplatform/stage/us-west-2/redshift/ore-dp-non-prod-redshift-cluster]
WARN[0030]                                               prefix=[/home/jmreicha/github.com/lytx/cloudops-terragrunt/dataplatform/stage/us-west-2/redshift/ore-dp-non-prod-redshift-cluster]
INFO[0030] All required inputs are passed in by terragrunt  prefix=[/home/jmreicha/github.com/lytx/cloudops-terragrunt/dataplatform/stage/us-west-2/redshift/ore-dp-non-prod-redshift-cluster]
ERRO[0030] Terragrunt configuration has misaligned inputs. Strict mode enabled: true.
ERRO[0030] Unable to determine underlying exit code, so Terragrunt will exit with error code 1

@jmreicha jmreicha changed the title feat: terragrunt input validate Terragrunt input validate hook Jun 8, 2024
@jmreicha jmreicha changed the title Terragrunt input validate hook feat: Terragrunt input validate hook Jun 8, 2024
Copy link
Collaborator

@yermulnik yermulnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (apart from unrelated formatting changes to README — please revert those unrelated changes, that were presumably applied automatically by your IDE)

ps: you may try and add the below to the config file of your VSCode to stop it from removing trailing dots from markdown

    "[markdown]": {
        "files.trimTrailingWhitespace": false
    }

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@MaxymVlasov
Copy link
Collaborator

Or just install https://editorconfig.org/ plugin to your favorite editor. I'll add it to the contrib article

@yermulnik
Copy link
Collaborator

Or just install editorconfig.org plugin to your favorite editor. I'll add it to the contrib article

Ah, right! Thanks, Max. I always miss that we have .editorconfig in here =)

@MaxymVlasov
Copy link
Collaborator

Here we go: https://github.com/antonbabenko/pre-commit-terraform/blob/master/.github/CONTRIBUTING.md#required-tools-and-plugins-to-simplify-review-process

If you merge the last changes to your branch, you'll be able to use extensions settings too.

Sorry for the inconvenience, that should have been added a long time ago.

@jmreicha
Copy link
Contributor Author

Thanks for comments, I’ll get the formatting fixed up.

@jmreicha
Copy link
Contributor Author

I think I got reformatted properly 🤞

Copy link
Collaborator

@yermulnik yermulnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Leaving final review to @MaxymVlasov

README.md Outdated
```

> [!NOTE]
> This hooks requires authentication to a given account if defined by config in order to work properly.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we provide here link to docs about authentication or provide more details?

I not sure which authentification is mentioned here. valid creds in ~/.aws/ ~/.azure/ etc? Or if not Cloud account, which account then?

Are there any terragrunt docs or issues about that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was considering opening an issue, in my testing I am using AWS and iam_role. I will see if I can find a relevant link.

Copy link
Contributor Author

@jmreicha jmreicha Jun 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. I will look at opening an issue separately in the Terragrunt project and update the note here if I get an answer.

I have a highly customized setup so most folks may not ever run into this.

@MaxymVlasov MaxymVlasov changed the title feat: Terragrunt input validate hook feat: Add terragrunt_validate_inputs hook to check unused and undefined inputs Jun 19, 2024
Copy link
Collaborator

@yermulnik yermulnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@MaxymVlasov MaxymVlasov merged commit a139b71 into antonbabenko:master Jun 19, 2024
6 checks passed
antonbabenko pushed a commit that referenced this pull request Jun 19, 2024
# [1.92.0](v1.91.0...v1.92.0) (2024-06-19)

### Features

* Add `terragrunt_validate_inputs` hook to check unused and undefined inputs ([#677](#677)) ([a139b71](a139b71))
@antonbabenko
Copy link
Owner

This PR is included in version 1.92.0 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Terragrunt validate-inputs hook?
4 participants