5

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
shonky linux user
  • 6,131
  • 4
  • 46
  • 73
Ammar Ameerdeen
  • 950
  • 3
  • 11
  • 29

1 Answers1

7

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.

shonky linux user
  • 6,131
  • 4
  • 46
  • 73
  • Tried this. When I did so, port 80 traffic also got redirected to 3000. – Ammar Ameerdeen Jan 02 '19 at 02:10
  • Strange, This matches my cloudformation template (except that I am using HTTPS Protocol) and it works correctly. Are you sure it isn't the backend redirecting rather than the ALB? – shonky linux user Jan 02 '19 at 02:54
  • You cannot have 2 listeners going to the same target group. This should be unique per listener. – tyron Jan 03 '19 at 01:43
  • 1
    @tyron AWS does allow multiple ALB listeners to point to the same target group. I have it setup that way in my own AWS account. But in the context of this question, it is ambiguous as to whether the OP needs target groups to hit different ports. In any case I have amended my answer to reflect this. – shonky linux user Jan 03 '19 at 03:17