antonbabenko / pre-commit-terraform

pre-commit git hooks to take care of Terraform configurations 🇺🇦
MIT License
3.16k stars 535 forks source link

tflint hook unable to ignore terraform_required_version and terraform_required_providers rules specified in .tflint.hcl file #530

Closed sunnygoel87 closed 1 year ago

sunnygoel87 commented 1 year ago

Describe the bug

I'm using terraform_tflint hook from this repo. in our project to identify the potential possible errors (before they occur) and ensure that Terraform code complies with best practices. I've specified the terraform version and aws provider version in main.tf file at root level but it seems, tflint expects us to specify the terraform block and aws provider in main.tf file under each module.

How can we reproduce it?

Leverage .pre-commit-config.yaml file as given below and .tflint.hcl file as shown below. Afterwards, have an infrastructure folder in your application, create a couple of modules and don't specify the terraform version and aws provider version in main.tf file under those modules. Run pre-commit run -a command locally in IDE and then you should be able to see the error message I pasted below.

file content ```bash plugin "terraform" { enabled = true preset= "recommended" } plugin "aws" { enabled = true version = "0.23.1" source = "github.com/terraform-linters/tflint-ruleset-aws" } rule "terraform_required_version" { enabled = false } rule "terraform_required_providers" { enabled = false } ```

Environment information

Darwin SGOEL1ML1 22.4.0 Darwin Kernel Version 22.4.0: Mon Mar  6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000 arm64
tflint --version
TFLint version 0.46.1
+ ruleset.terraform (0.2.2-bundled)

terraform --version
Terraform v1.4.4
on darwin_arm64
file content ```bash repos: - repo: https://github.com/antonbabenko/pre-commit-terraform rev: v1.80.0 # Get the latest from: https://github.com/gruntwork-io/pre-commit/releases hooks: - id: terraform_tflint args: - --args=--config=.tflint.hcl - --args=--module ```
sunnygoel87 commented 1 year ago

@antonbabenko - Could you please help me resolve this? Thanks a lot

antonbabenko commented 1 year ago

I think the path to .tflint.hcl is incorrect because now it expects such config in each directory. Try to specify __GIT_WORKING_DIR__/.tflint.hcl as described here.

sunnygoel87 commented 1 year ago

@antonbabenko - Thanks a lot for the prompt response. Moving .tflint.hcl file at the root level and passing the arg in .pre-commit-config.yaml file as you suggested fixed the issue.