Skip to content

How do I use a shared Application Load Balancer with multiple Elastic Beanstalk environments in AWS CDK?

0

I’m trying to configure multiple Elastic Beanstalk environments to share a single Application Load Balancer (ALB) using AWS CDK (v2).

In the AWS Console, there is an option when creating an environment to “use an existing load balancer” (Shared Load Balancer feature). However, when I try to do this via CDK, I can’t find a way to pass in the ALB ARN or listener ARN to Beanstalk.

I’ve tried setting optionSettings like this:

{
  namespace: "aws:elbv2:loadbalancer",
  optionName: "SharedLoadBalancer",
  value: "arn:aws:elasticloadbalancing:region:account:loadbalancer/app/my-shared-alb/..."
}

and even:

{
  namespace: "aws:elbv2:listener",
  optionName: "ListenerARN",
  value: "arn:aws:elasticloadbalancing:region:account:listener/app/my-shared-alb/.../..."
}

But I get this error:

Configuration validation exception: Invalid option specification 
(Namespace: 'aws:elbv2:listener', OptionName: 'null'): Unknown configuration setting.

So it seems CloudFormation/CDK doesn’t actually expose the shared ALB feature, even though it exists in the EB console.

My questions are:

Is there any supported way in CDK/CloudFormation to provision Elastic Beanstalk environments that share a single ALB?

If not, is the only workaround to use SingleInstance environments and manually attach them to a CDK-provisioned ALB target group?

Or do I need to drop Beanstalk and use ECS/App Runner if I want true IaC with shared ALBs?

Any guidance (or confirmation this isn’t possible in CDK) would be appreciated.

2 Answers
0
Accepted Answer

The missing namespace option is

        {
          namespace: 'aws:elasticbeanstalk:environment',
          optionName: 'LoadBalancerIsShared',
          value: 'true',
        },

Full config for shared beanstalk ALB should look something like this.

        {
          namespace: 'aws:elasticbeanstalk:environment',
          optionName: 'LoadBalancerType',
          value: 'application',
        },
        {
          namespace: 'aws:elasticbeanstalk:environment',
          optionName: 'LoadBalancerIsShared',
          value: 'true',
        },
        {
          namespace: 'aws:ec2:vpc',
          optionName: 'ELBSubnets',
          value: publicSubnets,
        },
        {
          namespace: 'aws:elbv2:loadbalancer',
          optionName: 'SharedLoadBalancer',
          value: props.sharedAlbArn,
        },

answered a year ago

-1

To use a shared Application Load Balancer with multiple Elastic Beanstalk environments, you need to configure the environment with the correct option settings in AWS CDK.

The shared Application Load Balancer feature was introduced by AWS Elastic Beanstalk in September 2020 to help save costs when you have multiple environments, such as in microservice architectures. Instead of each environment having its own dedicated load balancer, you can create and manage a load balancer yourself using Amazon EC2 and then share it across multiple Elastic Beanstalk environments.

In AWS CDK, you can configure a shared Application Load Balancer for an Elastic Beanstalk environment using the following option settings:

{
namespace: "aws:elbv2:loadbalancer",
optionName: "SharedLoadBalancer",
value: "arn:aws:elasticloadbalancing:us-east-2:123456789012:loadbalancer/app/MySharedALB/e574ea4c37ad2ec8"
}

You must also set the load balancer type to "application" in your environment configuration:

{
namespace: "aws:elasticbeanstalk:environment",
optionName: "LoadBalancerType",
value: "application"
}

Important requirements to note:

  1. Your environment must be in a VPC with subnets in at least two Availability Zones to use an Application Load Balancer.
  2. You need to create the shared Application Load Balancer first using Amazon EC2 before referencing it in your Elastic Beanstalk environments.
  3. When using a shared load balancer, you are responsible for creating and managing the load balancer, listeners, and listener rules, while Elastic Beanstalk manages the processes (target groups).

If you're encountering errors with the configuration, ensure that:

  • You've correctly specified the ARN of an existing Application Load Balancer
  • You've set the load balancer type to "application"
  • You're not trying to configure listener ARNs directly (as this isn't supported)

The shared load balancer approach is specifically designed for cost savings in scenarios with multiple environments, such as microservice architectures, where having dedicated load balancers for each environment would be costly.
Sources
Configuring a shared Application Load Balancer - AWS Elastic Beanstalk
eb create - AWS Elastic Beanstalk
Release: Elastic Beanstalk introduces shared Application Load Balancers on September 10, 2020 - AWS Elastic Beanstalk
Load balancer for your Elastic Beanstalk environment - AWS Elastic Beanstalk

answered a year ago

EXPERT

reviewed a year ago

  • but this still results in the stack creating new ALB's

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.