Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

share common data between AWS Lambda functions

If I have a set of common config like a set of constant used by a number of Lambda functions. Is there any way I can share the data between them in one place so that I can modify the values easily without doing update one-by-one for inside each lambda function?

I can put the data to a DB, but would cause one extra query for each lambda request and run slower.

like image 720
Nick Avatar asked Mar 14 '23 06:03

Nick


2 Answers

You can use Lambda Layers to share code between different lambda functions. The code in the lambda will be then available in each function in the /opt folder.

like image 30
H6. Avatar answered Mar 21 '23 05:03

H6.


If the config isn't going to change very often I would just throw it in S3 and have each lambda load it at startup.

If it does change a lot or you want to build some sort of UI to update the settings then you could go the DB route. If you load config when the lambda starts up - ie outside of the handler function you would only need to load the config once as long as the lambda doesn't go back to sleep so the penalty probably isn't that steep.

// this will only be loaded when the Lambda starts up
// keep in mind if you are loading from an external resource
// it will probably be async so your function should return a Promise
var config = someFunctionThatLoadsConfigFromS3();

// entry point for Lambda will be called for every event(api gateway request)
module.exports.handler = function(event, context) {
  config.then(function(config) {
    // do some stuff with config
    context.done(null, 'return a value');
  }).catch(....); 
};
like image 186
Ryan Avatar answered Mar 21 '23 04:03

Ryan