-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce LocalLambdaGroup to manage multiple lambdas in a sing…
…le server instance (#20) * feat: multiple handlers on same port * feat: Create new file for Lambda Group server * chore: address PR comments
- Loading branch information
1 parent
5570064
commit 5129e4a
Showing
4 changed files
with
121 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Context } from 'aws-lambda'; | ||
import { LambdaResponse, RequestEvent } from '../types'; | ||
import { LambdaConfig, LocalLambdaGroupConfig, LocalLambdaGroup } from '../lambda.group'; | ||
|
||
// handler is a function that takes in an event and context and returns a response | ||
const handler = async (req: RequestEvent, context: Context): Promise<LambdaResponse> => ({ statusCode: 200, body: `Hello World !!! My userId is ${req.pathParameters?.id}\n My JWT is ${JSON.stringify(req.requestContext.authorizer.lambda.jwt)}. My queryStringParameter is ${JSON.stringify(req.queryStringParameters)} ` }); | ||
const funnyHandler = async (req: RequestEvent, context: Context): Promise<LambdaResponse> => ({ statusCode: 200, body: `Hello World !!! I'm funny and my userId is ${req.pathParameters?.id}\n My JWT is ${JSON.stringify(req.requestContext.authorizer.lambda.jwt)}. My queryStringParameter is ${JSON.stringify(req.queryStringParameters)} ` }); | ||
|
||
// context is provided as optional field in config. | ||
const config: LambdaConfig[] = [ | ||
{ | ||
handler: handler, // if type is not compatible, do `handler: handler as any` | ||
requestContext: { | ||
authorizer: { | ||
lambda: { | ||
jwt: { | ||
claims: { | ||
sub: '1234567890', | ||
name: 'John Doe', | ||
iat: 1516239022, | ||
}, | ||
scopes: ['read', 'write'], | ||
}, | ||
}, | ||
}, | ||
}, | ||
pathParamsPattern: '/user/:id', // optional, default to '/' | ||
}, | ||
{ | ||
handler: funnyHandler, // if type is not compatible, do `handler: handler as any` | ||
pathParamsPattern: '/user/:id/funny', // optional, default to '/' | ||
requestContext: { | ||
authorizer: { | ||
lambda: { | ||
jwt: { | ||
claims: { | ||
sub: '1234567890', | ||
name: 'John Doe', | ||
iat: 1516239022, | ||
}, | ||
scopes: ['read', 'write'], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
const multiConfig: LocalLambdaGroupConfig = { | ||
lambdas: config, | ||
port: 8008, // optional, default to 8000 | ||
defaultPath: '/api/v1', // optional, default to '/' | ||
}; | ||
|
||
// visit http://localhost:8008/api/v1/user/1234567890 and http://localhost:8008/api/v1/user/1234567890/funny to see the response | ||
const localLambdaGroup = new LocalLambdaGroup(multiConfig); | ||
localLambdaGroup.run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './local.lambda'; | ||
export * from './types/index'; | ||
export * from './types/index'; | ||
export * from './lambda.group'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Context } from 'aws-lambda'; | ||
import express from 'express'; | ||
import { LambdaHandler } from 'index'; | ||
import { DefaultPathParamsPattern, DefaultPort, LocalLambda } from './local.lambda'; | ||
|
||
|
||
export interface LambdaConfig { | ||
handler: LambdaHandler; | ||
context?: Context; | ||
enableCORS?: boolean; | ||
// default binary content-type headers or override | ||
binaryContentTypesOverride?: string[]; | ||
pathParamsPattern ?: string; | ||
requestContext?: Record<string, any>; | ||
} | ||
|
||
|
||
export interface LocalLambdaGroupConfig { | ||
lambdas: LambdaConfig[]; | ||
port?: number; | ||
defaultPath?: string; | ||
} | ||
|
||
export class LocalLambdaGroup { | ||
lambdas: LambdaConfig[] = []; | ||
app: express.Application; | ||
port: number; | ||
defaultPath: string; | ||
|
||
constructor(config: LocalLambdaGroupConfig) { | ||
this.lambdas = config.lambdas; | ||
this.app = express(); | ||
this.port = config.port ?? DefaultPort; | ||
this.defaultPath = config.defaultPath ?? DefaultPathParamsPattern; | ||
} | ||
|
||
run(): void { | ||
this.lambdas.forEach(lambda => { | ||
const localLambda = new LocalLambda(lambda, this.app, this.defaultPath); | ||
localLambda.createRoute(); | ||
}); | ||
this.app.listen(this.port, () => console.info(`🚀 Lambda Group Server ready at http://localhost:${this.port} at '${new Date().toLocaleString()}'`)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters