Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS LoadBalancer to Listen on multiple ports

I have a few applications running as Microservices in aws. Some of them are running on port 80 and some of them are running on port 3000. I want my ALB to listen to traffic on both ports. Then I have a ListenRules to direct the traffic to Microservices. I want to achieve something like below,

Resources:
  LoadBalancer:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Name: !Ref EnvironmentName
      Subnets: !Ref Subnets
      SecurityGroups:
        - !Ref SecurityGroup
      Tags:
        - Key: Name
          Value: !Ref EnvironmentName

  LoadBalancerListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      LoadBalancerArn: !Ref LoadBalancer
      Port: [80,3000] # something like this
      Protocol: HTTP
      DefaultActions:
        - Type: forward
          TargetGroupArn: !Ref DefaultTargetGroup
like image 558
Ammar Ameerdeen Avatar asked Jan 01 '19 16:01

Ammar Ameerdeen


1 Answers

The Listener should be repeated with each port that is to be opened. For example:

Resources:
LoadBalancer:
  Type: AWS::ElasticLoadBalancingV2::LoadBalancer
  Properties:
    Name: !Ref EnvironmentName
    Subnets: !Ref Subnets
    SecurityGroups:
      - !Ref SecurityGroup
    Tags:
      - Key: Name
        Value: !Ref EnvironmentName

LoadBalancerListenerA:
  Type: AWS::ElasticLoadBalancingV2::Listener
  Properties:
    LoadBalancerArn: !Ref LoadBalancer
    Port: 80
    Protocol: HTTP
    DefaultActions:
      - Type: forward
        TargetGroupArn: !Ref TargetGroupForPort80

LoadBalancerListenerB:
  Type: AWS::ElasticLoadBalancingV2::Listener
  Properties:
    LoadBalancerArn: !Ref LoadBalancer
    Port: 3000
    Protocol: HTTP
    DefaultActions:
      - Type: forward
        TargetGroupArn: !Ref TargetGroupForPort3000

This also allows the flexibility of setting different protocols (e.g. HTTPS) or target groups for each port.

like image 83
shonky linux user Avatar answered Sep 28 '22 11:09

shonky linux user