Terraform:如何将子模块的输出变量传递给调用方模块



实际的例子很复杂,我试着简化一下:

variable "input" {
type = string
default  "In Xanadu did Khubla Khan"
}
module "child" {
source = "./child
input = var.input
}
output "output" {
value = module.child.output
}
现在子模块
variable "input" {
type = string
}
output "output" {
value = "${var.input} a stately pleasure dome decree"
}

结果我得到

╷
│ Error: Unsupported attribute
│
│   on output.tf line 122, in output "output":
│  122:   value       =  module.child.output
│     ├────────────────
│     │ module.child is a object, known only after apply
│
│ This object does not have an attribute named "output".

更新:我在这里使用了一个抽象的例子,变量名像"input"one_answers";output"并从引用变量"aws_s3_crossaccount_iam_role_arn"的实际地形脚本中复制错误消息。这显然让一些读者感到困惑。因此,我将错误信息中的真实名称替换为"output">

当然,错误信息取自一个真实的脚本,但我希望,在我上面的例子中,我会得到类似

的东西我已经在谷歌上搜索了这个问题,但没有得到任何线索。Terraform提供程序版本

C:Databricksdbx_tpch> terraform -version
Terraform v1.2.2
on windows_amd64
+ provider registry.terraform.io/databricks/databricks v1.14.2
+ provider registry.terraform.io/hashicorp/aws v4.62.0
+ provider registry.terraform.io/hashicorp/random v3.4.3
+ provider registry.terraform.io/hashicorp/time v0.9.1
Your version of Terraform is out of date! The latest version
is 1.4.4. You can update by downloading from https://www.terraform.io/downloads.html

我当然可以升级,但我不认为这有什么用

  1. 解决方案与其在根模块中重新定义子模块中的每个输出变量,不如引用整个子模块
output "single_child_output" {
description = "Do not use it"
value = module.child.child_output
}
output "all_child_outputs" {
description = "use this instead"
value = module.child
}

作为结果,你将得到一个地图

输出:

all_child_outputs = {
"variable1" = "value1"
"variable2" = "value2"
"variable3" = "value3"
}

重要-这个解决方案甚至在我升级terraform之前就起作用了

  1. 解决方案

然而,碰巧的是,我在排除故障的步骤中升级了地形本身

dbx_tpch> terraform version
Terraform v1.4.4
on windows_amd64
+ provider registry.terraform.io/databricks/databricks v1.14.2
+ provider registry.terraform.io/hashicorp/aws v4.62.0
+ provider registry.terraform.io/hashicorp/random v3.5.1
+ provider registry.terraform.io/hashicorp/time v0.9.1

错误消失了,我可以使用上面的两个变体-在子模块中地址单个输出变量或整个输出块。这两个工作。恕我直言,对整个块进行寻址有时更容易,但如果你想使用"地形输出"的输出。作为某些脚本的输入—最好使用单个变量。

最新更新