PrakashS
PrakashS

Reputation: 135

Getting invalid index and empty tuple error

I am trying to attach the target groups to the load balancer in terraform and I am getting the below error:

resource "aws_lb_target_group" "mytargetgroup" {
  count = var.environment_acronym == "pd" ? 1 : 0
  name                    = "My Target Group"
  port                    = 80
  protocol                = "HTTP"
  vpc_id                  = var.vpc_main_id

}

    resource "aws_lb_target_group_attachment" "mytargetgroup_attachment" {
      target_group_arn = aws_lb_target_group.mytargetgroup[0].arn  --> having issues here
      target_id        = var.private_admin
      port             = 80
   }

Error: Invalid index on lb_tg_attachments\lb_public_myloadbalancer_attach.tf line 70, in resource "aws_lb_target_group_attachment" "mytargetgroup_attachment": 70: target_group_arn = aws_lb_target_group.mytargetgroup[0].arn ├──────────────── │ aws_lb_target_group.mytargetgroup is empty tuple

  The given key does not identify an element in this collection value.

How can I avoid this error. Any suggestions are appreciated.

Upvotes: 1

Views: 3111

Answers (1)

Marcin
Marcin

Reputation: 238309

Your condition var.environment_acronym == "pd" must be false, so aws_lb_target_group does not exist. Thus it does not work. You have to check for it later on as well:

    resource "aws_lb_target_group_attachment" "mytargetgroup_attachment" {
      count = var.environment_acronym == "pd" ? 1 : 0

      target_group_arn = aws_lb_target_group.mytargetgroup[0].arn  --> having issues here
      target_id        = var.private_admin
      port             = 80
   }

Upvotes: 1

Related Questions