I want to create additional listeners for AWS Elastic Beanstalk environments that use a shared load balancer.
Short description
If you're using a shared load balancer with Elastic Beanstalk, then you can't create additional listeners using the aws:elbv2:listener:listener_port option setting or the Elastic Beanstalk console. This is because the load balancer isn't managed by Elastic Beanstalk.
You can use .ebextension custom resources to create additional listeners for an Elastic Beanstalk environment with a shared load balancer.
Tip: It's a best practice to associate additional listeners with the lifecycle of the environment, and to remove the listeners if you terminate the environment.
Resolution
1. Create an Application Load Balancer that includes a default listener and target group.
2. Create a configuration file called additional-listener.config file that includes the following:
Resources:
AdditionalHttpListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
LoadBalancerArn:
"Fn::GetOptionSetting":
Namespace: "aws:elbv2:loadbalancer"
OptionName: "SharedLoadBalancer"
DefaultActions:
- Type: forward
TargetGroupArn:
Ref: AWSEBV2LoadBalancerTargetGroup
Port: 8080
Protocol: HTTP
Note: The YAML file in step 2 follows the AWS CloudFormation specification for the AWS::ElasticLoadBalancingV2::Listener resource.
3. Place the file from step 2 into the .ebextensions folder that's part of your application source bundle.
4. Create a ZIP file of your updated application source bundle.
5. Use the ZIP file from step 4 to create a new Elastic Beanstalk environment, or update an existing environment that's configured with the shared load balancer from step 1.
The configuration file from step 2 creates an HTTP listener on port 8080 for the shared load balancer associated with your Elastic Beanstalk environment. Then, the listener forwards all traffic to the default process. You can further extend this configuration file to add additional rules to the listener using the AWS::ElasticLoadBalancingV2::ListenerRule resource definition of CloudFormation.
Important: Because this listener is created as an additional resource as part of the Elastic Beanstalk environment, the listener is removed if the environment is terminated.
Note: To learn more about shared load balancers and default listener rules, see Configuring a shared Application Load Balancer.