-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide a lambda context with logging/error handling included
- Loading branch information
Showing
5 changed files
with
105 additions
and
0 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
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,4 @@ | ||
export { Const } from './const'; | ||
export { Aws } from './aws/index'; | ||
|
||
export { LambdaFunction } from './lambda'; |
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,36 @@ | ||
import { Logger } from './log'; | ||
import { Context, Callback } from 'aws-lambda'; | ||
import * as pino from 'pino'; | ||
|
||
export class LambdaFunction { | ||
/** | ||
* Wrap a lambda function to provide extra functionality | ||
* | ||
* - Log metadata about the call on every request | ||
* - Catch errors and log them before exiting | ||
*/ | ||
static wrap<T, K>(fn: (event: T, context: Context, logger: pino.Logger) => Promise<K>) { | ||
return async (event: T, context: Context, callback: Callback<K>) => { | ||
const startTime = Date.now(); | ||
|
||
const lambda = { | ||
name: process.env['AWS_LAMBDA_FUNCTION_NAME'], | ||
memory: process.env['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'], | ||
version: process.env['AWS_LAMBDA_FUNCTION_VERSION'], | ||
region: process.env['AWS_REGION'], | ||
}; | ||
|
||
const logger = Logger.child({}); | ||
logger.info({ lambda }, 'LambdaStart'); | ||
|
||
try { | ||
const res = await fn(event, context, logger); | ||
logger.info({ lambda, duration: Date.now() - startTime, status: 200 }, 'LambdaDone'); | ||
callback(null, res); | ||
} catch (error) { | ||
logger.info({ lambda, duration: Date.now() - startTime, status: 500, error: error }, 'LambdaError'); | ||
callback(error); | ||
} | ||
}; | ||
} | ||
} |
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,3 @@ | ||
import * as pino from 'pino'; | ||
|
||
export const Logger = pino({ level: 'debug' }); |
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