Skip to content

ListenerRule

Source: src/AWS/ELBv2/ListenerRule.ts

An ELBv2 listener rule. Rules attach to an Application Load Balancer listener and route requests to target groups (or other actions) based on conditions such as host header, path pattern, HTTP header, query string, request method, and source IP.

Path-based routing

const rule = yield* ListenerRule("api", {
listenerArn: listener.listenerArn,
priority: 10,
conditions: [{ pathPattern: { values: ["/api/*"] } }],
actions: [
{ type: "forward", targetGroups: [{ targetGroupArn: apiTg.targetGroupArn }] },
],
});

Host-header routing

const rule = yield* ListenerRule("admin", {
listenerArn: listener.listenerArn,
priority: 20,
conditions: [{ hostHeader: { values: ["admin.example.com"] } }],
actions: [
{ type: "forward", targetGroups: [{ targetGroupArn: adminTg.targetGroupArn }] },
],
});
const rule = yield* ListenerRule("beta", {
listenerArn: listener.listenerArn,
priority: 30,
conditions: [
{ queryString: { values: [{ key: "version", value: "beta" }] } },
{ httpHeader: { name: "X-Channel", values: ["internal"] } },
],
actions: [{ type: "fixedResponse", statusCode: "200", messageBody: "beta" }],
});