Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share code between AWS lambda functions in node.js

It seems it is not possible to pass around some code (containing data and functions) that is invoked as a AWS lambda function within another AWS lambda function.

For example, take this customConfigLambda:

var callbackPayload = {};

callbackPayload.fooData = 'fooFromData';
callbackPayload.fooFunction = function() {return 'fooFromFunction'; };

exports.handler = (event, context, callback) => {
    callback(null, callbackPayload);
};

When I call the previous AWS lambda function in another AWS lambda function like here:

var AWS = require('aws-sdk');
AWS.config.update({accessKey: '123', secretAccessKey: 'abc', region: 'us-east-1' });
var lambda = new AWS.Lambda({region: 'us-east-1'});

exports.handler = (event, context, callback) => {
    var params = {FunctionName: 'customConfigLambda'};
    lambda.invoke(params, function(err, callbackPayload) {
        if (err) {
            // do nothing
        }
        else {
            console.log('callbackPayload:', JSON.stringify(callbackPayload, null, 2));
        }
    });
};

Then I can see only callbackPayload.fooData but not callbackPayload.fooFunction.

How can I have some callbackPayload.fooFunction(s) shared between multiple other AWS lambda functions?

like image 478
TPPZ Avatar asked Jun 21 '16 11:06

TPPZ


People also ask

Can AWS Lambda have multiple functions?

Serverless applications usually consist of multiple Lambda functions. Each Lambda function can use only one runtime but you can use multiple runtimes across multiple functions. This enables you to choose the best runtime for the task of the function.

Does AWS Lambda support node JS?

AWS Lambda now supports Node. js 16 as both a managed runtime and a container base image. Developers creating serverless applications in Lambda with Node. js 16 can take advantage of new features such as support for Apple silicon for local development, the timers promises API, and enhanced performance.


1 Answers

As of AWS Reinvent 2018, Amazon has introduced Lambda Layers.

Lambda Layers, a way to centrally manage code and data that is shared across multiple functions.

The idea is that now you can put common components in a ZIP file and upload it as a Lambda Layer. Your function code doesn’t need to be changed and can reference the libraries in the layer as it would normally do instead of packaging them separately.

like image 186
Jghorton14 Avatar answered Oct 10 '22 17:10

Jghorton14