Terraform 12 - 无法将 [count.index] 放入 outputs.tf

Terraform 12 - unable to place [count.index] in outputs.tf

我是 terraform 模块化部署的新手,我正在尝试在负载均衡器后面部署 aws lambda。以前我在尝试执行 "terraform validate"

时从目标组模块收到这些错误消息
Error: Missing resource instance key

  on ../modules/target-group/target-group.tf line 16, in resource "aws_lambda_permission" "lambda":
  16:   source_arn    = aws_alb_target_group.lambda.arn


Because aws_alb_target_group.lambda has "count" set, its attributes must be
accessed on specific instances.

For example, to correlate with indices of a referring resource, use:
    aws_alb_target_group.lambda[count.index]

是的,我按照建议的消息将 [count.index] 放置在目标组模块的依赖资源上。但是,当我尝试执行 "terraform validate" 时,它现在给了我在目标组模块的输出文件中添加的指令。发出此消息:

Error: Missing resource instance key

  on ../modules/target-group/outputs.tf line 6, in output "target_arn":
   6:  value ="${aws_alb_target_group.lambda.arn}"

Because aws_alb_target_group.lambda has "count" set, its attributes must be
accessed on specific instances.


For example, to correlate with indices of a referring resource, use:
    aws_alb_target_group.lambda[count.index]

所以我再次按照建议修改了这种格式的输出文件

output "target_arn" {
 value ="${aws_alb_target_group.lambda[count.index].arn}"
}

那么我现在收到的是这条消息:

Error: Reference to "count" in non-counted context

  on ../modules/target-group/outputs.tf line 6, in output "target_arn":
   6:  value ="${aws_alb_target_group.lambda[count.index].arn}"

The "count" object can be used only in "resource" and "data" blocks, and only
when the "count" argument is set.

他们之前添加 [count.index] 的建议不适用于输出文件。 我不再明白要调整什么,也看不到关于如何解决问题的在线解决方案。请在这里建议需要什么。

使用 outputs.tf

中的格式修复了问题
output "target_arn" {
 value =join("", aws_alb_target_group.lambda[*].arn)
}

另一种指定方式

output "event_bus_arn" {
    value = element(concat(aws_cloudwatch_event_bus.event_bus.*.arn, [""]), 0)
}