Skip to content

Listener

Source: src/AWS/ELBv2/Listener.ts

An ELBv2 (Application/Network) Load Balancer listener. A listener checks for connection requests using its configured protocol and port, then routes them to target groups via its default actions (and any attached ListenerRules).

Basic HTTP forward listener

const listener = yield* Listener("http", {
loadBalancerArn: lb.loadBalancerArn,
targetGroupArn: tg.targetGroupArn,
port: 80,
protocol: "HTTP",
});

HTTPS listener with certificate and SSL policy

const listener = yield* Listener("https", {
loadBalancerArn: lb.loadBalancerArn,
defaultActions: [
{ type: "forward", targetGroups: [{ targetGroupArn: tg.targetGroupArn }] },
],
port: 443,
protocol: "HTTPS",
certificates: [primaryCertArn, sniCertArn],
sslPolicy: "ELBSecurityPolicy-TLS13-1-2-2021-06",
});

Redirect HTTP to HTTPS

const redirect = yield* Listener("redirect", {
loadBalancerArn: lb.loadBalancerArn,
defaultActions: [
{ type: "redirect", statusCode: "HTTP_301", protocol: "HTTPS", port: "443" },
],
port: 80,
protocol: "HTTP",
});

Fixed response

const maintenance = yield* Listener("maintenance", {
loadBalancerArn: lb.loadBalancerArn,
defaultActions: [
{ type: "fixedResponse", statusCode: "503", contentType: "text/plain", messageBody: "down" },
],
port: 80,
});

Weighted forward with stickiness

const weighted = yield* Listener("weighted", {
loadBalancerArn: lb.loadBalancerArn,
defaultActions: [
{
type: "forward",
targetGroups: [
{ targetGroupArn: blue.targetGroupArn, weight: 90 },
{ targetGroupArn: green.targetGroupArn, weight: 10 },
],
stickiness: { enabled: true, durationSeconds: 3600 },
},
],
port: 80,
});
const mtls = yield* Listener("mtls", {
loadBalancerArn: lb.loadBalancerArn,
defaultActions: [
{ type: "forward", targetGroups: [{ targetGroupArn: tg.targetGroupArn }] },
],
port: 443,
protocol: "HTTPS",
certificates: [certArn],
mutualAuthentication: { mode: "verify", trustStoreArn: trustStore.trustStoreArn },
});