为什么我的 Terraform 输出在模块中不起作用? [英] Why is my Terraform output not working in module?

查看:13
本文介绍了为什么我的 Terraform 输出在模块中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的设置:

~$ tree
.
├── main.tf
└── modules
    └── world
        └── main.tf
~$ cat main.tf
output "root_module_says" {
    value = "hello from root module"
}
module "world" {
    source = "modules/world"
}
~$ cat modules/world/main.tf
output "world_module_says" {
    value = "hello from world module"
}

然后我运行:

~$ terraform get
~$ terraform apply

我希望在输出中看到 world_module_says,但我没有,我只看到 root_module_says.这真的很令人困惑,为什么?

I expect to see world_module_says in the outputs, but I do not, I only see root_module_says. This is really confusing as to why?

如果有帮助:

~$ terraform --version
v0.10.8

推荐答案

Terraform 只显示 root 的输出(默认为 v0.12 之前)https://www.terraform.io/docs/commands/output.html

Terraform only shows the output from root (by default pre v0.12) https://www.terraform.io/docs/commands/output.html

在 Terraform 0.12 之前,您可以通过以下方式从 world 模块获取输出:

Prior to Terraform 0.12 you can get the output from the world module with:

terraform output -module=world

我认为这里的逻辑是模块的输出将由 root 使用,如果您确实需要输出,那么您也可以在 root 中输出它,因此 main.tf 可能包含以下内容:

I think the logic here is that the output from the module would be consumed by root and if you actually needed the output then you'd output it in root too so main.tf might contain this:

output "root_module_says" {
    value = "hello from root module"
}
output "world_module_says" {
    value = "${module.world.world_module_says}"
}
module "world" {
    source = "modules/world"
}

从 Terraform 0.12 开始,这是从模块中获取输出的唯一方法.

Beginning with Terraform 0.12 this is the only way to get the output from within a module.

这篇关于为什么我的 Terraform 输出在模块中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆