Single Lambda Function File Multiple Methods in NodeJS
AWS Lambda

Single Lambda Function File Multiple Methods in NodeJS

tldr; put multiple export methods in a single lambda function with this file

We all know the aws lambda front-end is quite unorganized and thus I find it frustratingly clunky, creating small function files inside aws for every little thing. Wouldn’t it be more efficient to create a single function file and house multiple exported methods inside?

Well here’s a template you can work with to save your time:

'use strict';

/*
Sample Input:
{
"function": "template_function"
}

*/

module.exports.handler = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;

if (typeof event.function === "undefined") {
callback(new Error('function name is undefined'));
}

if (typeof this[event.function] !== "function") {
callback(new Error(event.function + ' is not a valid function'));
}

try {
this[event.function](event, callback);
}
catch (error) {
callback(error);
}
finally {
console.log(event.function + ' done');
}

};

module.exports.template_function = (event, callback) => {
non_export_function('hello');
callback(null, 'Hello from Lambda');
};

var non_export_function = (args) => {
console.log('this is a local function');
};

The gist of it

Previous Post Next Post

You Might Also Like

No Comments

Leave a Reply

%d bloggers like this: