AWSTemplateFormatVersion: "2010-09-09"
Description: DT load balancing stack
Parameters:
  environment:
    Type: String
    Description: Name of the environment
    Default: production
  release:
    Type: String
    Description: Name of the release name of the stack version to use.
    Default: production
  PublicSubnets:
    Description: The public subnets for the ALB to run in.
    Type: String
  VpcId:
    Description: ID of the VPC
    Type: AWS::EC2::VPC::Id

Resources:

  #-- Network Load Balancer --#
  PublicNLB:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Type: network
      LoadBalancerAttributes:
        - Key: deletion_protection.enabled
          Value: false
      Scheme: internet-facing
      Subnets: !Split [",", !Ref PublicSubnets]
      Tags:
        - Key: Name
          Value: !Sub "DT-${environment}-NLB"
        - Key: environment
          Value: !Ref environment

  NlbTargetGroup:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      HealthCheckProtocol: TCP
      HealthCheckPort: 80
      Port: 7777
      Protocol: TCP_UDP
      TargetGroupAttributes:
        - Key: deregistration_delay.timeout_seconds
          Value: '20'
      VpcId: !Ref 'VpcId'
      Tags:
        - Key: Name
          Value: !Sub 'DT-${release}'

  NlbListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      DefaultActions:
        - Type: forward
          TargetGroupArn: !Ref NlbTargetGroup
      LoadBalancerArn: !Ref PublicNLB
      Port: 7777
      Protocol: TCP_UDP


Outputs:
  TargetGroup:
    Description: ""
    Value: !Ref NlbTargetGroup
  NlbDnsName:
    Description: ""
    Value: !GetAtt PublicNLB.DNSName