pre-commit-terraform icon indicating copy to clipboard operation
pre-commit-terraform copied to clipboard

Support tflint modules with differing module dependencies

Open davidjb opened this issue 2 years ago β€’ 8 comments

What problem are you facing?

In my configuration, I have a number of different modules in the same repo but each module has differing dependencies (either local or published). At present, I'm linting by running cd {dir} && terraform get && tflint for each module directory to ensure all dependencies are available and installed. When trying to use the terraform_tflint hook, it fails for modules with dependencies.

How could pre-commit-terraform help solve your problem?

Have the ability to automatically invoke terraform get per-directory when running the tflint hook, either by default or with a flag. Whilst terraform get is a noop when nothing needs to be done, it still takes time to be invoked so an opt-in hook flag may be the better option.

Happy to help craft a PR if this is something that could be incorporated.

davidjb avatar Jun 20 '23 04:06 davidjb

You can do next as a workaround (because t init took more time than t get)

- id: terraform_validate
  args:
    - --hook-config=--retry-once-with-cleanup=true
    # - --tf-init-args=-upgrade
- id: terraform_tflint
  args:
    - --args=--config=__GIT_WORKING_DIR__/.tflint.hcl

Also, because tflint and t validate can't fully replace each other, it can be a solution, not a temporary workaround

MaxymVlasov avatar Jun 20 '23 19:06 MaxymVlasov

Thanks @MaxymVlasov - I appreciate the workaround. It is fairly slow to re-init Terraform each tune, as you say, especially when multiple modules and configurations are changing in a given commit, but workable for small changes.

davidjb avatar Jun 21 '23 02:06 davidjb

Side note: terraform_providers_lock could use t get too, rather depend on terraform_validate

MaxymVlasov avatar Jun 21 '23 18:06 MaxymVlasov

What should be done:

  1. create by analog common::terraform_get function

    https://github.com/antonbabenko/pre-commit-terraform/blob/ae5cbe0342617310b0491146d51573f2f4c93705/hooks/_common.sh#L316

  2. Include it in terraform_tflint by analogue how common::terraform_init used in terraform_validate hook

  3. (Optional) Add it to terraform_providers_lock hook too

MaxymVlasov avatar Jun 21 '23 18:06 MaxymVlasov

Not sure if anyone is aware, but TFLint 0.50.0 has some specific changes relating to the handling of modules and has marked that change as a BC (but is documented more like a deprecation notice).

https://github.com/terraform-linters/tflint/releases/tag/v0.50.0

Hopefully, the work undertaken there can be integrated/supported by pre-commit-terraform.

rquadling avatar Jan 02 '24 16:01 rquadling