Giter VIP home page Giter VIP logo

Comments (6)

antonbabenko avatar antonbabenko commented on July 18, 2024

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.

from pre-commit-terraform.

SolidSly avatar SolidSly commented on July 18, 2024

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.

from pre-commit-terraform.

kdimiche avatar kdimiche commented on July 18, 2024

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.

from pre-commit-terraform.

jmcshane avatar jmcshane commented on July 18, 2024

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.

from pre-commit-terraform.

antonbabenko avatar antonbabenko commented on July 18, 2024

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

from pre-commit-terraform.

antonbabenko avatar antonbabenko commented on July 18, 2024

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.

from pre-commit-terraform.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.