Question:
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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 |
Answer:
The Listener should be repeated with each port that is to be opened. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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.