Skip to content

Commit 8c55081

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

File tree

9 files changed

+410
-2
lines changed

9 files changed

+410
-2
lines changed

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
commands = {
2729
offline: {
2830
// add start nested options
@@ -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 },
@@ -260,6 +282,7 @@ export default class ServerlessOffline {
260282
const lambdas = []
261283
const scheduleEvents = []
262284
const webSocketEvents = []
285+
const albEvents = []
263286

264287
const functionKeys = service.getAllFunctions()
265288

@@ -273,7 +296,7 @@ export default class ServerlessOffline {
273296
const events = service.getAllEventsInFunction(functionKey) || []
274297

275298
events.forEach((event) => {
276-
const { http, httpApi, schedule, websocket } = event
299+
const { http, httpApi, schedule, websocket, alb } = event
277300

278301
if ((http || httpApi) && functionDefinition.handler) {
279302
const httpEvent = {
@@ -353,6 +376,14 @@ export default class ServerlessOffline {
353376
websocket,
354377
})
355378
}
379+
380+
if (alb) {
381+
albEvents.push({
382+
alb,
383+
functionKey,
384+
handler: functionDefinition.handler,
385+
})
386+
}
356387
})
357388
})
358389

@@ -370,6 +401,7 @@ export default class ServerlessOffline {
370401
}
371402

372403
return {
404+
albEvents,
373405
httpEvents,
374406
lambdas,
375407
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
corsAllowHeaders: 'accept,content-type,x-api-key,authorization',
67
corsAllowOrigin: '*',

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)