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

terraform validate pre-commit hook fails for modules #42

Open
johannes-mathes opened this issue Nov 18, 2020 · 8 comments
Open

terraform validate pre-commit hook fails for modules #42

johannes-mathes opened this issue Nov 18, 2020 · 8 comments
Labels
question Further information is requested

Comments

@johannes-mathes
Copy link

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 brikis98 added the question Further information is requested label Nov 20, 2020
@brikis98
Copy link
Member

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

@thibault-ketterer
Copy link

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
Copy link

thibault-ketterer commented Dec 3, 2020

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

@davidalger
Copy link
Contributor

@thibault-ketterer Ran into this same issue myself this morning, and it seems it's an issue with the provider (see hashicorp/terraform-provider-aws#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
Copy link

thanks I'll check that

@scott1138
Copy link

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
Copy link

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
Copy link

I created #114 to describe a similar issue

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

No branches or pull requests

7 participants