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

terraform validate pre-commit hook fails for modules

Open johannes-mathes opened this issue 4 years ago • 8 comments

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?

johannes-mathes avatar Nov 18 '20 11:11 johannes-mathes

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

brikis98 avatar Nov 20 '20 12:11 brikis98

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 avatar Dec 03 '20 11:12 thibault-ketterer

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

thibault-ketterer avatar Dec 03 '20 11:12 thibault-ketterer

@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

davidalger avatar Dec 08 '20 17:12 davidalger

thanks I'll check that

thibault-ketterer avatar Dec 08 '20 20:12 thibault-ketterer

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?

scott1138 avatar May 01 '22 21:05 scott1138

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>/[^/]+$

MichaelAicher avatar Jul 20 '22 09:07 MichaelAicher