resource "aws_lb" "dad-dev-tusd-ext-lb" {
  #checkov:skip=CKV2_AWS_28: Ensure public facing ALB are protected by WAF
  name                                        = "dad-dev-tusd-ext-lb"
  internal                                    = false
  load_balancer_type                          = "application"
  security_groups                             = [aws_security_group.AMP-DIGARCH-D-TUSD-SG.id]
  subnets                                     = [aws_subnet.subnet-05c45da01c1648f71.id, aws_subnet.subnet-08ecf11e742dfe0bd.id]
  enable_cross_zone_load_balancing            = "false"
  enable_deletion_protection                  = "true"
  enable_http2                                = true
  enable_tls_version_and_cipher_suite_headers = false
  enable_waf_fail_open                        = false
  enable_xff_client_port                      = false
  idle_timeout                                = 600
  preserve_host_header                        = false
  xff_header_processing_mode                  = "append"
  access_logs {
    bucket  = "amp-dev-elb-logs"
    enabled = true
    prefix  = "887829410671/dad-dev-tusd-ext-lb"
  }
  tags = {
    Terraform     = "True"
    Business-Unit = "AMP"
    Environment   = "Dev"
    Owner         = "nathaniel.lovett@sonymusic.com"
    Project-Code  = "SME-AMP"
    Project-Name  = "Digital Archive"
  }
}

resource "aws_lb_target_group" "dad-dev-tusd-ext-lb-8080" {
  #checkov:skip=CKV_AWS_378: Ensure AWS Load Balancer doesn't use HTTP protocol
  count       = 1
  name        = "dad-dev-tusd-ext-lb-8080"
  target_type = "instance"
  port        = 8080
  protocol    = "HTTP"
  vpc_id      = aws_vpc.vpc-09c83633eb7b315a2.id
  health_check {
    healthy_threshold   = 2
    interval            = 30
    unhealthy_threshold = 2
    timeout             = 5
    path                = "/index.html"
    port                = 8080
  }
}

resource "aws_lb_target_group_attachment" "dad-dev-tusd-ext-lb-8080" {
  count            = 1
  target_group_arn = aws_lb_target_group.dad-dev-tusd-ext-lb-8080[count.index].arn
  target_id        = aws_instance.ampdad-tusd01.id
  port             = 8080
}

resource "aws_lb_listener" "dad-dev-tusd-ext-lb" {
  count             = 1
  load_balancer_arn = aws_lb.dad-dev-tusd-ext-lb.arn
  port              = 443
  protocol          = "HTTPS"
  ssl_policy        = "ELBSecurityPolicy-TLS13-1-2-2021-06"
  certificate_arn   = "arn:aws:acm:us-east-1:887829410671:certificate/c3217562-9df8-4f8e-a9b3-1079d92794e1"
  default_action {
    type             = "forward"
    target_group_arn = aws_lb_target_group.dad-dev-tusd-ext-lb-8080[count.index].arn
  }
}