【问题标题】:Terraform 12 - unable to place [count.index] in outputs.tfTerraform 12 - 无法将 [count.index] 放入 output.tf
【发布时间】:2020-05-23 20:20:34
【问题描述】:

我是 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] 的建议不适用于输出文件。 我不再明白要调整什么,也无法查看有关如何解决问题的在线解决方案。请在这里提出需要的建议。

【问题讨论】:

    标签: lambda terraform terraform-provider-aws


    【解决方案1】:

    使用 outputs.tf 中的格式修复的问题

    output "target_arn" {
     value =join("", aws_alb_target_group.lambda[*].arn)
    }
    

    【讨论】:

      【解决方案2】:

      另一种指定方式

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

      【讨论】:

        猜你喜欢
        • 2018-10-22
        • 2021-08-06
        • 2019-01-23
        • 2020-01-16
        • 2018-10-15
        • 2018-09-09
        • 1970-01-01
        • 1970-01-01
        • 2020-07-07
        相关资源
        最近更新 更多