resource "aws_lb" "this" {
  name               = var.name
  load_balancer_type = var.load_balancer_type
  subnets            = local.subnet_list
  security_groups    = local.security_groups
}

resource "aws_lb_target_group" "this" {
  name_prefix = "tg-ci-"
  port        = var.port
  protocol    = var.protocol
  vpc_id      = var.vpc_id
  target_type = var.target_type

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_lb_listener" "this" {
  load_balancer_arn = aws_lb.this.id
  port              = var.port
  protocol          = var.protocol

  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.this.arn
  }

  depends_on = [aws_lb_target_group.this]
}