'How can I use the output of a module that is located at outer level of the caller module?

I'm working with terraform for a while and I didn't face any issues as I always followed a simple structure. But now I've started working on another project that doesn't seem like it adheres to best practices.

The project structure is like this:

> root-folder
   > modules
      > cloudfront
      > route53
      > team1
         > service1
            - main.tf
            - variables.tf
            - locals.tf

And what I'm trying to do is, use modules.cloudfront.some_output_value within the modules>team1>service1>main.tf file as seen below:

module "record" {
  source      = "../../route53"
  zone_id     = var.route53_zone_id
  record_name = local.record_name
  cdn_id       = module.cloudfront.some_output_value   //the question is about this line
}

But I'm unable to use it, since the IDE indicates there is no such module as module.cloudfront.

Does anyone know why I cannot use a module that is defined at an outer scope?

Terraform version: 1.2.0 (probably this's not related to a version based issue, but anyway.)



Solution 1:[1]

You can't query outer modules from inner modules. If you have any variables/outputs in your outermodules that should be used in the inner modules, you have to explicitly pass them from the outer into inner modules.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcin