HandlerWrapper

HandlerWrapper

Utility class that creates wrappers for AWS Lambda functions by wrapping a simple Node.js function that serves as a lambda handler. These wrappers initialize and inject configuration and logger objects, and also allow underlying implementations to return promises for asynchronous operations.

Constructor

new HandlerWrapper(appName)

Source:
Parameters:
Name Type Description
appName String The name of the application to which the lambda functions returned by this object belong. This value will be injected into log statements emitted by the logger.

Methods

wrap(handler, handlerName) → {HandlerWrapper.Wrapper}

Source:
Creates and returns a wrapper lambda handler.
Parameters:
Name Type Description
handler HandlerWrapper.Handler A function that contains the intended lambda functionality.
handlerName String The name of the handler. Used for logging.
Returns:
A function that can be used as the AWS lambda handler.
Type
HandlerWrapper.Wrapper

Type Definitions

Handler(event, contex, ext) → {Promise|*}

Source:
A function that contains the core execution logic of the lambda function. This function receives the input and context from the AWS lambda, along with some extended properties, and can return any value, including a Promise for asynchronous operations.
Parameters:
Name Type Description
event Object The input to the lambda function, not altered in any way by the wrapper.
contex Object The AWS lambda context, not altered in any way by the wrapper.
ext Object Extended parameters passed to the handler. These are values injected by the wrapper, providing utility objects that the handler can optionally utilize.
Properties
Name Type Description
config Object A properly scoped configuration object. This object contains configuration parameters for a specific environment, based on the lambda alias value.
logger Object A logger object that can be used to write log messages. The logger object is pre initialized with some metadata that includes the application name, lambda handler name and the lamnda execution id. More properties may be added to it if necessary by invoking logger.child().
alias String The alias with which the lambda function was invoked. If the lambda was not invoked unqualified or as latest version ($LATEST), the alias value will be set to "default".
Returns:
The response from the lambda handler execution. If a promise is returned, the the wrapper will wait for resolution or rejection of the promise, and use the corresponding result as the response value of the lambda.
Type
Promise | *

Wrapper(event, contex, callback)

Source:
A function that conforms to the AWS signature. This function initializes the logger and config objects, and delegates actual execution to a handler function.

The wrapper also provides lambda keep warm capability that can be used to keep the lambda in cache without actually invoking the handler function. This can be accomplished by passing a parameter called __LAMBDA_KEEP_WARM with a truthy value to the wrapper.

Parameters:
Name Type Description
event Object The input to the lambda function, not altered in any way by the wrapper.
contex Object The AWS lambda context.
callback function The callback function passed to the AWS lambda.