Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

To use AWS Lambda (Serverless) with RDS in production, you should

  • warm up functions
  • preserve DRS connections

configure_handlers.js configures Serverless handler functions to achieve them.

Usage

const configure_handlers = require('./configure_handlers');

function create_user(event, context, handler_callback) {
  // Write you function
}

function list_users(event, context, handler_callback) {
  // Write you function
}

module.exports = configure_handlers({
  create_user,
  list_users,
});

References

'use strict';
function configure_handler(handler) {
return (event, context, handler_callback) => {
// ref: https://www.jeremydaly.com/reuse-database-connections-aws-lambda/
context.callbackWaitsForEmptyEventLoop = false;
// ref: https://github.com/FidelLimited/serverless-plugin-warmup
if (event.source === 'serverless-plugin-warmup') {
// TODO: Modify the response in accordance with your needs
return handler_callback(null, { statusCode: 204 });
}
return handler(event, context, handler_callback);
};
}
function configure_handlers(handlers) {
const configured_handlers = {};
Object.keys(handlers).forEach((handler_name) => {
configured_handlers[handler_name] = configure_handler(handlers[handler_name]);
});
return configured_handlers;
}
module.exports = configure_handlers;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment