How can I access value of tuple?

Viewed 13822

I would like to know how can I access the value from the following output:

output "backend_tg_arn" {
  value = var.backend_tg_needed ? aws_lb_target_group.backend_tg[*].arn : null
}

It is generated with the following code:

resource "aws_lb_target_group" "backend_tg" {
  count       = var.backend_tg_needed ? 1 : 0
  name        = "${var.client_code}-${var.environment}-backend-tg"
  port        = var.backend_port
  protocol    = "HTTP"
  target_type = var.backend_target_type
  vpc_id      = aws_vpc.main_vpc.id
}

The output from above is passed to another module as variable:

   backend_tg_arn = module.network.backend_tg_arn

and I'm trying to assign it to default action for alb listener:

  default_action {
    type             = "forward"
    target_group_arn = var.backend_tg_arn
  }
}

Getting error:

var.backend_tg_arn is tuple with 1 element

Inappropriate value for attribute "target_group_arn": string required.

I am struggling how can I access this arn value... it should be done with some kind of loop but can't figure it out.

1 Answers

You are always setting a count of 1 so you can instead just output the first element if it's created:

output "backend_tg_arn" {
  value = var.backend_tg_needed ? aws_lb_target_group.backend_tg[0].arn : null
}
Related