Ammar Ameerdeen
Ammar Ameerdeen

Reputation: 969

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

Upvotes: 5

Views: 4010

Answers (1)

shonky linux user
shonky linux user

Reputation: 6428

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.

Upvotes: 7

Related Questions