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

Validate without variables fails when there is a subfolder present with TF code #59

Closed
5obol opened this issue Aug 6, 2019 · 6 comments

Comments

@5obol
Copy link

5obol commented Aug 6, 2019

Hi
My modules contains examples/ folder with implementation example. After upgrade of pre-commit-terraform and my code to 0.12.x the validate without variables stopped working for subfolders complaining that module wasn't initialised but it actually was.

My hook config:

- repo: git://github.com/antonbabenko/pre-commit-terraform
  rev: v1.17.0
  hooks:
    - id: terraform_fmt
    - id: terraform_docs
    - id: terraform_validate

Error:
pre-commit run -a
Terraform fmt............................................................Passed
Terraform docs...........................................................Passed
Terraform validate without variables.....................................Failed
hookid: terraform_validate

Success! The configuration is valid.


Failed path: examples
================================

Error: Module not installed

  on examples/main.tf line 6:
   6: module "s3_bucket" {

This module is not yet installed. Run "terraform init" to install all modules
required by this configuration.
@antonbabenko
Copy link
Owner

Hi Przemyslaw!

Thanks for opening this issue. Yes, terraform validate has slightly changed behavior in 0.12.

$ terraform validate -help

...

  Validation requires an initialized working directory with any referenced
  plugins and modules installed. To initialize a working directory for
  validation without accessing any configured remote backend, use:
      terraform init -backend=false

  To verify configuration in the context of a particular run (a particular
  target workspace, operation variables, etc), use the terraform plan
  subcommand instead, which includes an implied validation check.

I have not tried to reproduce the issue you are talking about, and I am not sure what we can do with this in this project (and whether we should?).

Please share your ideas.

@SolidSly
Copy link

SolidSly commented Sep 26, 2019

Greetings,

Sorry to "reopen" this issue after two months. But I ran into kind of same problem today.
I will try to be as explicit as possible so everyone can understand how to reproduce it.

Architecture

.
├── parent_module_1
│   │
│   ├── child_module
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   └── variables.tf
│   │
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
│
├── parent_module_2
│   │
│   ├──child_module
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   └── variables.tf
│   │ 
│   ├── main.tf
│   ├── outputs.tf
│   └── variables.tf
│
├── main.tf
├── outputs.tf
└── variables.tf

Problem

If you have a module inside another module and that the modules are all defined with a source type local_path; then the pre-commit terraform validate will not work and return errors.

It is kind of logical since in the documentation of Terraform, it is written that the local_path modules are not really installed.

Of course, I try all the basics, such as terraform get, terraform init, terraform init -upgrade etc...

Errors

Error: Module not installed

  on main.tf line 32:
  32: module "test" {

This module's local cache directory  could not be read. Run "terraform init"
to install all modules required by this configuration.

Suggestion

I discovered that the terraform validate packaged inside the 0.12.9 version of Terraform is working properly.

If you have any suggestion or workaround to help us with this problem, thank you for sharing them.

@kdimiche
Copy link

kdimiche commented Oct 2, 2019

This may be a known issue with Terraform. See hashicorp/terraform#21769. Specifically:

With all of this said, we recommend not relying on the directory arguments to these commands in general, because in any configuration that uses path.module and/or path.root it can cause details about the specific system running Terraform to be exposed into the state, making the configuration unportable to run on different systems. By far the most common and recommended way is to cd into the configuration directory and run all commands from there without any overriding path arguments.

Manually, I validated that running terraform validate works after changing directories:

[AutoScaling]$ terraform validate
Success! The configuration is valid.

[AutoScaling]$ echo $?
0
[AutoScaling]$ cd test/
[test]$ terraform validate
Success! The configuration is valid.

[test]$ echo $?
0

While

[AutoScaling]$ terraform validate test

Error: Module not installed

  on test/main.tf line 5:
   5: module "autoscaling" {

This module is not yet installed. Run "terraform init" to install all modules
required by this configuration.

[AutoScaling]$ terraform validate test

Error: Module not installed

  on test/main.tf line 5:
   5: module "autoscaling" {

This module is not yet installed. Run "terraform init" to install all modules
required by this configuration.

@jmcshane
Copy link

So, reading into hashicorp/terraform#21769, running terraform validate against a subdirectory or module is just not something that is supported. I have to remove this check from my pre-commit configuration for now given the repository structure that I need to support.

Does terraform validate from the root validate all referenced files from submodules? At least variable references, I believe it does.

@antonbabenko
Copy link
Owner

terraform_validate hook has been just updated in #100, and v1.29.0 is now released. Please give it a try!

@antonbabenko
Copy link
Owner

Closing this issue.

I have released an updated version (v1.44.0 ) where terraform_validate runs terraform init if no .terraform folder found.

See #158 and updated README.md file.

terraform-aws-modules/terraform-aws-lambda#68 - here you can see how I use both terraform_validate and terraform_tflint.

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

No branches or pull requests

5 participants