Skip to content

Commit fb9f5b5

Browse files
docs(elb): document ALB multi-listener port-based routing (DOC-310) (#772)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 1f5e51c commit fb9f5b5

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

  • src/content/docs/aws/services

src/content/docs/aws/services/elb.mdx

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,81 @@ With the alternative URL structure:
170170
http(s)://localhost.localstack.cloud:4566/_aws/elb/example-lb/test/path
171171
```
172172

173+
## Multiple Listeners and Port-Based Routing
174+
175+
An Application Load Balancer can have multiple listeners, each bound to a different port.
176+
In LocalStack, same-scheme listeners (for example, two HTTP listeners on ports 80 and 8080) are routed by matching the request's arrival port to the listener's configured port.
177+
178+
### Configuring LocalStack for multiple listener ports
179+
180+
To reach two same-scheme listeners on distinct ports, both ports must be published in [`GATEWAY_LISTEN`](/aws/configuration/config/configuration/#core) when starting LocalStack:
181+
182+
```bash
183+
GATEWAY_LISTEN=0.0.0.0:4566,0.0.0.0:80,0.0.0.0:8080 localstack start
184+
```
185+
186+
### Creating multiple listeners
187+
188+
With LocalStack running and both ports published, create a load balancer and two HTTP listeners on different ports.
189+
The following example uses the `subnet_id` variable set in the [Getting started](#getting-started) steps above, and creates two listeners with distinct `fixed-response` default actions so you can verify that each port routes to the correct listener:
190+
191+
```bash
192+
# Create the load balancer
193+
loadBalancer=$(awslocal elbv2 create-load-balancer \
194+
--name multi-listener-lb \
195+
--subnets $subnet_id | jq -r '.LoadBalancers[].LoadBalancerArn')
196+
197+
# Listener on port 80
198+
awslocal elbv2 create-listener \
199+
--load-balancer-arn $loadBalancer \
200+
--protocol HTTP \
201+
--port 80 \
202+
--default-actions '{"Type":"fixed-response","FixedResponseConfig":{"StatusCode":"200","MessageBody":"Listener 80","ContentType":"text/plain"}}'
203+
204+
# Listener on port 8080
205+
awslocal elbv2 create-listener \
206+
--load-balancer-arn $loadBalancer \
207+
--protocol HTTP \
208+
--port 8080 \
209+
--default-actions '{"Type":"fixed-response","FixedResponseConfig":{"StatusCode":"200","MessageBody":"Listener 8080","ContentType":"text/plain"}}'
210+
```
211+
212+
A request to port 80 is handled by the listener bound to that port:
213+
214+
```bash
215+
curl multi-listener-lb.elb.localhost.localstack.cloud:80
216+
```
217+
218+
```bash title="Output"
219+
Listener 80
220+
```
221+
222+
A request to port 8080 is handled by the listener bound to that port:
223+
224+
```bash
225+
curl multi-listener-lb.elb.localhost.localstack.cloud:8080
226+
```
227+
228+
```bash title="Output"
229+
Listener 8080
230+
```
231+
232+
### By-design limitation: shared gateway port
233+
234+
When a request arrives on the shared `:4566` gateway port, LocalStack cannot determine which same-scheme listener was intended and falls back to the first-created listener:
235+
236+
```bash
237+
curl multi-listener-lb.elb.localhost.localstack.cloud:4566
238+
```
239+
240+
```bash title="Output"
241+
Listener 80
242+
```
243+
244+
:::note
245+
If your setup uses only the default `:4566` gateway port and you need multiple listeners, consolidate to a single listener per scheme. Same-scheme listeners on distinct ports can only be told apart when those ports are added to `GATEWAY_LISTEN` and targeted directly.
246+
:::
247+
173248
## Examples
174249

175250
The following code snippets and sample applications provide practical examples of how to use ELB in LocalStack for various use cases:

0 commit comments

Comments
 (0)