如何使用位于呼叫者模块外层的模块的输出?
我正在与Terraform合作一段时间,但我一直没有遇到任何问题,因为我总是遵循一个简单的结构。 但是现在,我开始研究另一个似乎并没有遵守最佳实践的项目。
项目结构就是这样:
> root-folder
> modules
> cloudfront
> route53
> team1
> service1
- main.tf
- variables.tf
- locals.tf
我要做的是,使用模块
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
}
。无法使用它,因为IDE表示 no module.cloudfront 。
有人知道为什么我不能使用在外部范围上定义的模块吗?
Terraform版本:1.2.0(这可能与基于版本的问题无关,但无论如何。)
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于您调用
cloudfront
模块的位置。选项1
如果您调用两个模块,
service1
和cloudfront
从同一父母进行值:service1
(应将其分配到variables.tf
和main.tf
当然。)parent模块
(例如,在root文件夹中)选项2
如果您从
service1
调用它,则文件层次结构中模块的位置无关紧要。您只需要正确设置源路径:It depends from where you invoke the
cloudfront
module.Option 1
If you invoke both modules,
service1
andcloudfront
from the same parent, then you need to define a variable inservice1
and explicitly pass the value:service1
(Should be splitted intovariables.tf
andmain.tf
of course.)parent module
(e.g. in root folder)Option 2
If you invoke it from
service1
, then the location of the module in the file hierarchy does not matter. You just have to set the source path right:您无法从内部模块查询外部模块。如果您的OUTERMODULE中有任何变量/输出应在内部模块中使用,则必须明确地将它们从外部传递到内部模块。
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.