如何使用位于呼叫者模块外层的模块的输出?

发布于 2025-01-31 08:22:41 字数 756 浏览 5 评论 0原文

我正在与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情丝乱 2025-02-07 08:22:41

这取决于您调用cloudfront模块的位置。

选项1

如果您调用两个模块,service1cloudfront从同一父母进行值:

service1 (应将其分配到variables.tfmain.tf当然。)

variable "cdn_id" {
  type        = ...
  description = "..."
}

module "record" {
  source       = "../../route53"
  zone_id      = var.route53_zone_id
  record_name  = local.record_name
  cdn_id       = var.cdn_id
}

parent模块 (例如,在root文件夹中)

module "service1" {
  source        = "modules/team1/service1"
  cdn_id        = module.cloudfront.some_output_value
  some_var_name = some_value
  ...
}

module "cloudfront" {
  source        = "modules/cloudfront"
  some_var_name = some_value
  ...
}

选项2

如果您从service1调用它,则文件层次结构中模块的位置无关紧要。您只需要正确设置源路径:

module "record" {
  source       = "../../route53"
  zone_id      = var.route53_zone_id
  record_name  = local.record_name
  cdn_id       = module.cloudfront.some_output_value
}

module "cloudfront" {
  source        = "../../cloudfront"
  some_var_name = some_value
  ...
}

It depends from where you invoke the cloudfront module.

Option 1

If you invoke both modules, service1 and cloudfront from the same parent, then you need to define a variable in service1 and explicitly pass the value:

service1 (Should be splitted into variables.tf and main.tf of course.)

variable "cdn_id" {
  type        = ...
  description = "..."
}

module "record" {
  source       = "../../route53"
  zone_id      = var.route53_zone_id
  record_name  = local.record_name
  cdn_id       = var.cdn_id
}

parent module (e.g. in root folder)

module "service1" {
  source        = "modules/team1/service1"
  cdn_id        = module.cloudfront.some_output_value
  some_var_name = some_value
  ...
}

module "cloudfront" {
  source        = "modules/cloudfront"
  some_var_name = some_value
  ...
}

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:

module "record" {
  source       = "../../route53"
  zone_id      = var.route53_zone_id
  record_name  = local.record_name
  cdn_id       = module.cloudfront.some_output_value
}

module "cloudfront" {
  source        = "../../cloudfront"
  some_var_name = some_value
  ...
}
夜夜流光相皎洁 2025-02-07 08:22:41

无法从内部模块查询外部模块。如果您的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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文