Search code examples
amazon-web-servicesterraformamazon-ecs

Terraform does not attach alb target group to load balancer


I have load balancer, target group, alb listener, and aws ecs services like below

resource "aws_alb" "django_alb" {
  name = "fpstory-django-alb"
  subnets = aws_subnet.public_subnet.*.id
  security_groups = [aws_security_group.django_lb.id]

  tags = {
    Project = "fpstory"
    Stage = terraform.workspace
  }
}

resource "aws_alb_target_group" "django_tg" {
  name = "fpstory-django-alb-target-group"
  port = 80
  protocol = "HTTP"
  vpc_id = aws_vpc.main.id
  target_type = "ip"

  depends_on = [
    aws_alb.django_alb
  ]

  health_check {
    healthy_threshold = "3"
    interval = "30"
    protocol = "HTTP"
    matcher = "200"
    port = var.django_port
    timeout = "5"
    path = var.health_check_path
    unhealthy_threshold = "3"
  }

  tags = {
    project = "fpstory"
    stage = terraform.workspace
  }
}

resource "aws_alb_listener" "django" {
  load_balancer_arn = aws_alb.django_alb.arn
  port = "80"
  protocol = "HTTP"
//  certificate_arn = var.certificate_arn

  default_action {
    target_group_arn = aws_alb_target_group.django_tg.id
    type = "redirect"

    redirect {
      status_code = "HTTP_301"
      protocol = "HTTP"
      port = var.django_port
    }
  }

}

resource "aws_ecs_service" "django" {
  name = "${terraform.workspace}-fpstory-django-service"
  cluster = aws_ecs_cluster.main.id
  task_definition = aws_ecs_task_definition.django.arn
  desired_count = var.django_count
  launch_type = "FARGATE"

  depends_on = [
    aws_alb_target_group.django_tg,
    aws_iam_role_policy_attachment.ecs_task_execution_role
  ]

  network_configuration {
    security_groups = [
      aws_security_group.django_ecs_tasks.id
    ]
    subnets = aws_subnet.public_subnet.*.id

    assign_public_ip = true
  }

  load_balancer {
    target_group_arn = aws_alb_target_group.django_tg.arn
    container_name = var.django_container_name
    container_port = var.django_port
  }

  tags = {
    Project = "fpstory"
    Stage = terraform.workspace
  }
}

When I create the infras running terraform apply I get following error.

InvalidParameterException: The target group with targetGroupArn arn:aws:elasticloadbalancing:ap-northeast-2:985371652497:targetgroup/fpstory-django-alb-target-group/537aa9c2afb0c38a does not have an associated load balancer. "stag-fpstory-django-service"

When I look at the AWS console, target groups are indeed not associated with any load balancer. Is there something else that I have to specify in order to associate the target group with load balancer?


Solution

  • I think you'd like to specify "forward" as a action type.

    In cace of "redirect", you don't need a target group.

    resource "aws_alb_listener" "django" {
      load_balancer_arn = aws_alb.django_alb.arn
      port = "80"
      protocol = "HTTP"
    //  certificate_arn = var.certificate_arn
    
      default_action {
        target_group_arn = aws_alb_target_group.django_tg.id
        type             = "forward"
      }
    }