How can I create additional listeners for Elastic Beanstalk environments that use a shared load balancer?

2 minute read
1

I want to create additional listeners for AWS Elastic Beanstalk environments that use a shared load balancer.

Short description

If you use a shared load balancer with Elastic Beanstalk, then you can't use the aws:elbv2:listener:listener_port option or the Elastic Beanstalk console to create additional listeners. This is because Elastic Beanstalk doesn't manage the load balancer.

To create additional listeners for an Elastic Beanstalk environment with a shared load balancer, use .ebextension custom resources.

Note: It's a best practice to associate additional listeners with the lifecycle of the environment. Remove the listeners if you terminate the environment.

Resolution

To create additional listeners for an Elastic Beanstalk environment with a shared load balancer, do the following:

  1. Create an Application Load Balancer that includes a default listener and target group.

  2. Create a YAML configuration file called additional-listener.config:

    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 preceding YAML configuration file follows the AWS CloudFormation specification for the AWS::ElasticLoadBalancingV2::Listener resource.

  3. Add the configuration file to the .ebextensions folder that's part of your application source bundle.

  4. Create a .zip file of your updated application source bundle.

  5. To create a new Elastic Beanstalk environment, use the .zip file. Or, update an existing environment that's configured with the shared load balancer from the previous step.

The configuration file creates an HTTP listener on port 8080 for the shared load balancer that's associated with your Elastic Beanstalk environment. Then, the listener forwards all traffic to the default process. To add rules to the listener, use the AWS::ElasticLoadBalancingV2::ListenerRule resource definition to extend the configuration file. Because the listener is an additional resource to the Elastic Beanstalk environment, the listener is removed if the environment is terminated.

Related information

Configuring a shared Application Load Balancer

AWS OFFICIAL
AWS OFFICIALUpdated 2 months ago
2 Comments

Looks like the indentation on the YAML example is messed up.

Could someone fix that?

Should probably look like this:

Resources:
  AdditionalHttpListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      DefaultActions:
        - Type: forward
          TargetGroupArn:
            Ref: AWSEBV2LoadBalancerTargetGroup
      LoadBalancerArn:
        "Fn::GetOptionSetting":
          Namespace: "aws:elbv2:loadbalancer"
          OptionName: "SharedLoadBalancer"
      Port: 8080
      Protocol: HTTP

Also worth mentioning that AWSEBV2LoadBalancerTargetGroup is described in the docs customize-containers-format-resources-eb

Dennis
replied 3 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 3 months ago