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

Module within module silently ignored until terraform init #21771

Closed
benhoskings opened this issue Jun 18, 2019 · 5 comments · Fixed by #22098
Closed

Module within module silently ignored until terraform init #21771

benhoskings opened this issue Jun 18, 2019 · 5 comments · Fixed by #22098

Comments

@benhoskings
Copy link

benhoskings commented Jun 18, 2019

Terraform Version

0.12.2

G'day, I've found on terraform 0.12.x that a module within a module won't be detected during plan and will be silently ignored until terraform init is run, rather than failing plan like a top-level module does.

Terraform Configuration Files

$ cat target/target.tf
resource "null_resource" "test" {
  provisioner "local-exec" {
    command = "true"
  }
}

module "mod" {
  source = "../module"
}
$ cat module/module.tf
resource "null_resource" "mod_test" {
  provisioner "local-exec" {
    command = "true"
  }
}
$ cat submod/submod.tf
resource "null_resource" "submod_test" {
  provisioner "local-exec" {
    command = "true"
  }
}

If you initialise the above structure, and then modify module/module.tf like so without re-initing:

$ cat module/module.tf
resource "null_resource" "mod_test" {
  provisioner "local-exec" {
    command = "true"
  }
}

module "submod" {
  source = "../submod"
}

Expected behaviour

Error: Module not installed

  on module.tf line 7:
   7: module "submod" {

Actual behaviour

$ terraform plan

# ...

Terraform will perform the following actions:

  # null_resource.test will be created
  + resource "null_resource" "test" {
      + id = (known after apply)
    }

  # module.mod.null_resource.mod_test will be created
  + resource "null_resource" "mod_test" {
      + id = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

$ terraform init

# ...

$ terraform plan

# ...

Terraform will perform the following actions:

  # null_resource.test will be created
  + resource "null_resource" "test" {
      + id = (known after apply)
    }

  # module.mod.null_resource.mod_test will be created
  + resource "null_resource" "mod_test" {
      + id = (known after apply)
    }

  # module.mod.module.submod.null_resource.submod_test will be created
  + resource "null_resource" "submod_test" {
      + id = (known after apply)
    }

Plan: 3 to add, 0 to change, 0 to destroy.
@jbardin
Copy link
Member

jbardin commented Jun 19, 2019

HI @benhoskings,

Thanks for filing the issue. This may seem unusual at first, but this is the expected behavior as new modules always need to be initialized via init. Terraform needs to analyze the module, ensure that all versions constraints match, the providers are available, and record the module location for reference.

The init command by itself is meant to be idempotent, and can be run whenever you have a change to the configuration to ensure everything is correctly initialized.

@jbardin jbardin closed this as completed Jun 19, 2019
@benhoskings
Copy link
Author

@jbardin Absolutely, I understand that the init is required; the problem is that terraform doesn't. :)

I would say terraform should fail with an error in this case, as shown in "expected output" above; instead it silently omits module changes.

@jbardin
Copy link
Member

jbardin commented Jun 19, 2019

Apologies @benhoskings, I saw your "expected output" and missed that it wasn't what you got ;).
That is unusual, we'll have to take a closer look.

@jbardin jbardin reopened this Jun 19, 2019
@gchamon
Copy link

gchamon commented Jul 15, 2019

My first thought when I came across this issue was that modules within modules wasn't supported, because while terraform silently ignores modules within modules, it fails instantly if the module is declared in the project's root folder.

It is actually just that terraform won't search for submodules within modules when planning to ask for terraform init, just for modules declared in .tf files in the root folder.

@ghost
Copy link

ghost commented Aug 16, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Aug 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants