InvalidParameterException: the target group with targetGroupArn xxxxx does not have an associated load balancerAW

Hey Guys,

The context is that I am trying to deploy a Fargate service with the load balancer setup, but I keep getting this error although I already followed the discussion at [AWS] Creating of ALB, Target Group and Service in one script causes errors · Issue #12634 · hashicorp/terraform · GitHub

Currently, a snippet of my code

resource "aws_lb" "main" {
  name               = local.name
  internal           = local.load_balancer_is_internal ? true : false
  load_balancer_type = "application"
  security_groups    = [aws_security_group.lb.id]
  subnets            = local.load_balancer_subnet_ids

  tags = merge(
    {
      Name = "${local.name}-lb-app_main"
    },
    local.tags
  )
}

resource "aws_lb_target_group" "main" {
  name        = local.name
  port        = local.app_port
  protocol    = "HTTP"
  vpc_id      = local.vpc_id
  target_type = "ip"

  health_check {
    protocol = "HTTP"
    matcher  = "200-202"
    path     = "/health"
  }

  depends_on = [
    aws_lb.main,
  ]
}

resource "aws_ecs_cluster" "app_main" {
  ...
}

resource "aws_ecs_task_definition" "app_main" {
  ...
}

resource "aws_ecs_service" "app_main" {
  name             = local.name
  cluster          = aws_ecs_cluster.app_main.id
  task_definition  = aws_ecs_task_definition.main.arn
  desired_count    = 2
  launch_type      = "FARGATE"
  platform_version = "1.4.0"

  network_configuration {
    subnets         = local.service_subnet_ids
    security_groups = [aws_security_group.ecs_service.id]
  }

  load_balancer {
    target_group_arn = aws_lb_target_group.main.arn
    container_name   = "app-container"
    container_port   = local.app_port
  }

  lifecycle {
    ignore_changes = [desired_count]
  }

  depends_on = [aws_lb_target_group.main]

  tags = merge(
    {
      Name = "${local.name}-ecs-service"
    },
    local.tags
  )
}

I tried creating fake dependencies, I tried to create depends on whenever possible, but the error still exists. I kinda getting frustrated. Is there anyone with this issue that already solve this problem? Maybe I missed other solution

Any help would be greatly appreciated.

Okay this is my bad. I forgot to provision a aws_lb_listener resource. Once I provisioned it, it works.