1
votes

I'm kinda newbie to terraform modular-kind of deployment and I'm trying to deploy aws lambda behind a load balancer. Previously I'm getting these error messages from the target group module when trying to perform "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]

And yes I followed the suggested message to place [count.index] on the dependency resource in the target group module. However when I tried to perform "terraform validate" it now gives me instruction to add also on the outputs file from the target group module. Giving this message:

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]

So again as suggested I modified the outputs file in this format

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

Then what I'm getting now is this message:

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.

Their previous suggestion to add [count.index] is not usable to outputs file. I no longer understand what to adjust and unable to see online solutions on how to solve the issue. Please suggest on what is need here.

2

2 Answers

1
votes

issue fixed by using the format in outputs.tf

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

Another way to specify

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