How to Use One Load Balancer for Multiple Elastic Beanstalk Environments (Production + Sandbox)

How to Use One Load Balancer for Multiple Elastic Beanstalk Environments (Production + Sandbox)

Written by
Written by

Sagar J.

Post Date
Post Date

Nov 25, 2025

shares

When working with multiple AWS Elastic Beanstalk environments (Production, Staging, Sandbox), the monthly cost can rise quickly — mainly because each environment spins up its own Application Load Balancer (ALB).

But what if you could run both Production and Sandbox behind a single ALB?

Spoiler: Yes, you can.
And this can save $300+ per year — per environment.

This guide covers:
   1. Why sharing an ALB makes sense
   2. Architecture overview
   3. Step-by-step configuration
   4. Using AWS Certificate Manager (ACM) for HTTPS
   5. Cost savings
   6. Common pitfalls

Let’s dive in.

Why Share One Load Balancer?

By default, Elastic Beanstalk creates one ALB per environment.

Each ALB typically costs:

If your Sandbox or Staging environment doesn’t need high scalability, you can:

Benefits

Architecture Overview

1_N9mgxjswkA5N_owvfiK4RA

We will configure:

Step-by-Step Setup Guide

Step 1: Create the Sandbox Environment (Single Instance)

In Elastic Beanstalk:

This gives a cost-efficient sandbox with only 1 EC2 instance.

Step 2: Create a Target Group for Sandbox

Since Sandbox has no ALB, EB will not create a Target Group.

You must create it manually.

Step 3: Add Domain Routing Rules to ALB

You do not need a new 443 listener.
Use the existing one from Production.

IF Host header = sandbox.example.com

THEN Forward  SANDBOX TARGET GROUP

Step 4: Add DNS Record for Sandbox Domain

In Route53:

Create CNAME:

sandbox.example.com your-ALB-dns-name.amazonaws.com

ALB will route the traffic.

Using AWS Certificate Manager (ACM) for HTTPS

ACM certificates cannot be installed on EC2 instances or Single Instance EB.

They only work with:

So the ALB will handle all HTTPS termination.

Step 1: Request a Certificate in ACM

Request a new Public Certificate.

Add domains:

Validate using DNS CNAME.

Step 2: Attach Certificate to ALB Listener

Now HTTPS works for all domains pointing to ALB.

Step 3: Optional — Redirect HTTP → HTTPS

Add rule:

At your backend:

IF protocol = HTTP
THEN Redirect
HTTPS

Cost Benefits

1__UEeiJo4t6ntinuAOTUA6A

Total Savings

~$300/year per environment

Common Pitfalls & Fixes

ALB Health Checks Failing

Allow ALB Security Group → Sandbox EC2 instance

SSL Not Working for Sandbox

Add sandbox.example.com to the ACM certificate

Routing Goes to Production

Increase Sandbox rule priority

Sandbox Not Reachable

Ensure EC2 instance is in a public subnet

Real Issue We Faced: Sandbox Instance Not Attaching to Target Group

When using a Single Instance Elastic Beanstalk Sandbox, we discovered a major issue:

If the EC2 instance becomes degraded and EB replaces it, the new instance does NOT automatically attach to the custom target group.

Why? Because the target group was created manually.
EB only auto-registers instances for EB-managed ALBs, not for manually created ones.

What happened:

This is a frequent and often overlooked issue in this architecture.

Solution: Auto-Register Sandbox EC2 Instances Using a Post-Deploy Hook

Instead of using Lambda/EventBridge, we applied a simpler solution inside the EB environment itself, using a post-deploy hook that runs every time EB launches or replaces an instance.

How the Fix Works

We created:

.platform/hooks/postdeploy/01-register-target.sh

This script:

Full Script

#!/bin/bash # Your target group ARN TARGET_GROUP_ARN="arn:aws:elasticloadbalancing:ap-south-1:123456789012:targetgroup/sandbox-tg/abc123" # Get instance ID INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) IS_REGISTERED=$(aws elbv2 describe-target-health \ --target-group-arn "$TARGET_GROUP_ARN" \ --query "TargetHealthDescriptions[?Target.Id=='$INSTANCE_ID']" \ --output text) if [ -n "$IS_REGISTERED" ]; then echo "Instance $INSTANCE_ID is already registered to Target Group $TARGET_GROUP_ARN" exit 0 fi # Register instance to Target Group aws elbv2 register-targets \ --target-group-arn $TARGET_GROUP_ARN \ --targets Id=$INSTANCE_ID echo "Instance $INSTANCE_ID registered to Target Group $TARGET_GROUP_ARN"

Required IAM Permissions

To allow the script to interact with ELB, we added an inline policy to the:

aws-elasticbeanstalk-ec2-service-role

Inline IAM Policy

{   "Version": "2012–10–17",   "Statement": [     {       "Effect": "Allow",       "Action": [         "elasticloadbalancing:DescribeTargetHealth",         "elasticloadbalancing:RegisterTargets"       ],       "Resource": "*"     }   ] }

After adding the policy, we redeployed the environment so a new instance picked up the updated role.

Outcome

With this fix:

This makes the shared ALB setup fully stable for Sandbox workflows.

Final Thoughts

Using one Application Load Balancer for multiple Elastic Beanstalk environments is one of the most effective AWS cost-saving designs — while maintaining full HTTPS support via ACM.

You get:

If you’re running multiple EB environments, this setup is a must-implement architecture.

Important Note (Please Read Before Implementation)

Before you apply this architecture, please remember:

This article is for educational purposes, and we recommend double-checking configurations to avoid downtime or unexpected behavior.