resource "aws_lb" "wsi_alb" {
  name = "wsi-alb"
  internal = false
  load_balancer_type = "application"
  security_groups = [
    aws_security_group.alb_sg.id
  ]
  subnets = [
    aws_subnet.public_a.id,
    aws_subnet.public_b.id,
    aws_subnet.public_c.id
  ]
}

resource "aws_lb_target_group" "wsi_alb_tg" {
  name = "wsi-app-tg"
  port = 8080
  protocol = "HTTP"
  vpc_id = aws_vpc.main.id
  target_type = "instance"

  health_check {
    path                = "/health"
    interval            = 5
    timeout             = 2
    healthy_threshold   = 2
    unhealthy_threshold = 2
    matcher             = "200"
  }
}

resource "aws_lb_listener" "wsi_alb_listener" {
  load_balancer_arn = aws_lb.wsi_alb.arn
  port = 80
  protocol = "HTTP"
  default_action {
    type = "forward"
    target_group_arn = aws_lb_target_group.wsi_alb_tg.arn
  }

  lifecycle {
    ignore_changes = [default_action]
  }
}

resource "aws_lb_target_group_attachment" "wsi_alb_tg_attachment" {
  count            = length(aws_instance.wsi_app)
  target_group_arn = aws_lb_target_group.wsi_alb_tg.arn
  target_id        = aws_instance.wsi_app[count.index].id
  port             = 8080
}