I would like to add custom middleware to do things like count API requests and other analytics type things. Is there a way to do this in Serverless. Ideally it would be similar to setting up a Custom Authorizer, but able to work for any function.
Before you spend too much time building metrics into your Lambda’s have a look at CloudWatch.
Lambda doesn’t really have the concept of middleware. If you need more then export a new function that implements the middleware and have it call your old function.
oldHandler = (event, context, callback) => {
// Your original handler here
}
exports.newHandler = (event, context, callback) => {
// Start tracking
oldHandler(event, context, (err, success) => {
// Log here
callback(err, success);
});
}
1 Like