Skip to content

Commit f82f164

Browse files
committed
Implemented ALB support
Based on: - dherault#1250 by bayoumymac - https://github.com/tmaslen/serverless-local-alb-example by tmaslen
1 parent c250a8c commit f82f164

9 files changed

Lines changed: 413 additions & 2 deletions

File tree

src/ServerlessOffline.js

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export default class ServerlessOffline {
2323

2424
#webSocket = null
2525

26+
#alb = null
27+
2628
constructor(serverless, cliOptions) {
2729
this.#cliOptions = cliOptions
2830
this.#serverless = serverless
@@ -61,7 +63,7 @@ export default class ServerlessOffline {
6163
async start() {
6264
this.#mergeOptions()
6365

64-
const { httpEvents, lambdas, scheduleEvents, webSocketEvents } =
66+
const { httpEvents, lambdas, scheduleEvents, webSocketEvents, albEvents } =
6567
this.#getEvents()
6668

6769
// if (lambdas.length > 0) {
@@ -70,6 +72,10 @@ export default class ServerlessOffline {
7072

7173
const eventModules = []
7274

75+
if (albEvents.length > 0) {
76+
eventModules.push(this.#createAlb(albEvents))
77+
}
78+
7379
if (httpEvents.length > 0) {
7480
eventModules.push(this.#createHttp(httpEvents))
7581
}
@@ -99,6 +105,10 @@ export default class ServerlessOffline {
99105
eventModules.push(this.#lambda.stop(SERVER_SHUTDOWN_TIMEOUT))
100106
}
101107

108+
if (this.#alb) {
109+
eventModules.push(this.#alb.stop(SERVER_SHUTDOWN_TIMEOUT))
110+
}
111+
102112
if (this.#http) {
103113
eventModules.push(this.#http.stop(SERVER_SHUTDOWN_TIMEOUT))
104114
}
@@ -210,6 +220,18 @@ export default class ServerlessOffline {
210220
return this.#webSocket.start()
211221
}
212222

223+
async #createAlb(events, skipStart) {
224+
const { default: Alb } = await import('./events/alb/index.js')
225+
226+
this.#alb = new Alb(this.#serverless, this.#options, this.#lambda)
227+
228+
this.#alb.create(events)
229+
230+
if (!skipStart) {
231+
await this.#alb.start()
232+
}
233+
}
234+
213235
#mergeOptions() {
214236
const {
215237
service: { custom = {}, provider },
@@ -264,6 +286,7 @@ export default class ServerlessOffline {
264286
const lambdas = []
265287
const scheduleEvents = []
266288
const webSocketEvents = []
289+
const albEvents = []
267290

268291
const functionKeys = service.getAllFunctions()
269292

@@ -277,7 +300,7 @@ export default class ServerlessOffline {
277300
const events = service.getAllEventsInFunction(functionKey) || []
278301

279302
events.forEach((event) => {
280-
const { http, httpApi, schedule, websocket } = event
303+
const { http, httpApi, schedule, websocket, alb } = event
281304

282305
if ((http || httpApi) && functionDefinition.handler) {
283306
const httpEvent = {
@@ -357,6 +380,14 @@ export default class ServerlessOffline {
357380
websocket,
358381
})
359382
}
383+
384+
if (alb) {
385+
albEvents.push({
386+
alb,
387+
functionKey,
388+
handler: functionDefinition.handler,
389+
})
390+
}
360391
})
361392
})
362393

@@ -374,6 +405,7 @@ export default class ServerlessOffline {
374405
}
375406

376407
return {
408+
albEvents,
377409
httpEvents,
378410
lambdas,
379411
scheduleEvents,

src/config/commandOptions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
export default {
2+
albPort: {
3+
type: 'string',
4+
usage: 'ALB port to listen on. Default: 3003',
5+
},
26
apiKey: {
37
type: 'string',
48
usage:

src/config/defaultOptions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createApiKey } from '../utils/index.js'
22

33
export default {
4+
albPort: 3003,
45
apiKey: createApiKey(),
56
corsAllowCredentials: true, // TODO no CLI option
67
corsAllowHeaders: 'accept,content-type,x-api-key,authorization',

src/events/alb/Alb.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import AlbEventDefinition from './AlbEventDefinition.js'
2+
import HttpServer from './HttpServer.js'
3+
4+
export default class Alb {
5+
#httpServer = null
6+
7+
constructor(serverless, options, lambda) {
8+
this.#httpServer = new HttpServer(serverless, options, lambda)
9+
}
10+
11+
start() {
12+
return this.#httpServer.start()
13+
}
14+
15+
stop(timeout) {
16+
return this.#httpServer.stop(timeout)
17+
}
18+
19+
#createEvent(functionKey, rawAlbEventDefinition) {
20+
const albEvent = new AlbEventDefinition(rawAlbEventDefinition)
21+
22+
this.#httpServer.createRoutes(functionKey, albEvent)
23+
}
24+
25+
create(events) {
26+
events.forEach(({ functionKey, alb }) => {
27+
this.#createEvent(functionKey, alb)
28+
})
29+
30+
this.#httpServer.writeRoutesTerminal()
31+
}
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { assign } = Object
2+
3+
export default class AlbEventDefinition {
4+
constructor(rawAlbEventDefinition) {
5+
let listenerArn
6+
let priority
7+
let conditions
8+
let rest
9+
10+
if (typeof rawAlbEventDefinition === 'string') {
11+
;[listenerArn, priority, conditions] = rawAlbEventDefinition.split(' ')
12+
} else {
13+
;({ listenerArn, priority, conditions, ...rest } = rawAlbEventDefinition)
14+
}
15+
16+
this.listenerArn = listenerArn
17+
this.priority = priority
18+
this.conditions = conditions
19+
20+
assign(this, rest)
21+
}
22+
}

0 commit comments

Comments
 (0)