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.
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.
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(....);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With