gruntwork-io / pre-commit

A collection of pre-commit hooks used by Gruntwork tools
https://gruntwork.io/
Apache License 2.0
479 stars 155 forks source link

terraform validate pre-commit hook fails for modules #42

Open johannes-mathes opened 3 years ago

johannes-mathes commented 3 years ago

Dear Team,

I think your pre-commit-collection is really great. I would like to use also the terraform validate pre-commit hook and this fails for plain vanilla terraform modules (without terragrunt), because e.g. arguments are not set (because it is a module). Could we suppress tf validate if it is a module, meaning no backend definition is there or no provider is present?

brikis98 commented 3 years ago

Could you share the error you're getting? AFAIK, terraform validate should not have errors just because input vars aren't set...

thibault-ketterer commented 3 years ago

Got the same issue here

Initializing provider plugins...
- Using previously-installed hashicorp/aws v3.19.0

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.

* hashicorp/aws: version = "~> 3.19.0"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Error: Missing required argument

The argument "region" is required, but was not set.
thibault-ketterer commented 3 years ago

I forked and added fgrep -v modules in your script for now

davidalger commented 3 years ago

@thibault-ketterer Ran into this same issue myself this morning, and it seems it's an issue with the provider (see https://github.com/hashicorp/terraform-provider-aws/issues/9989)

You can workaround it by either adding a provider config:

provider "aws" {
  region  = "us-east-1"
}

OR by setting a default region in the execution environment:

export AWS_DEFAULT_REGION=us-east-1
thibault-ketterer commented 3 years ago

thanks I'll check that

scott1138 commented 2 years ago

I know this has been open for a while, but rather than create a new issue, I thought I'd use it. I have a modules folder that contains a few modules, one of which uses multiple providers. Because of this, validate cannot be run on this particular module. I'm not great with bash and I am new to pre-commit, so I was hoping for a little help. Can you add a way to exclude a "modules" folder or pass an exclusion when the hook is called?

MichaelAicher commented 2 years ago

I know this has been open for a while, but rather than create a new issue, I thought I'd use it. I have a modules folder that contains a few modules, one of which uses multiple providers. Because of this, validate cannot be run on this particular module. I'm not great with bash and I am new to pre-commit, so I was hoping for a little help. Can you add a way to exclude a "modules" folder or pass an exclusion when the hook is called?

You could exclude like this:

- repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.72.1
    hooks:
      - id: terraform_fmt
      - id: terraform_validate
        exclude: modules/<module-name>/[^/]+$
wyardley commented 2 months ago

I created #114 to describe a similar issue