SLS/AWS Lambda/Bolt JS question: since Lambda glob...
# help
a
SLS/AWS Lambda/Bolt JS question: since Lambda global Scope variables are shared across invocations, shouldn’t the express server and/or any other related vars be initialized inside the handler? Are there any best practices when it comes to SLS/ Slack / Bolt / AWS Lambda Global scope leaks? Thanks in advance! 🙏🏻
I’m having this issue where I schedule Slack messages and then a few hours later, they are deleted and someone else’s messages are scheduled on my account. I’ve wrapped all my local data JSON variables structure imports (requires) in functions but I still have the express server initialized outside the handler. I’m worried this is causing server leaks across invocations.
f
Hi @Andy Averbuch, it’s hard to tell what’s going on without digging into the code. Your best bet is
console.log
every event request and every api to Slack. And pull thru the logs when the issue happens again.
a
Hi @Frank thanks for getting back. The logs suggest that there’s a leak, I’m just worried that anything that’s initialized outside of the handler is leaking. Was wondering if there’s any experience with global leaks in Lambda and if there is a best practices guidance
s
Hey Andy, I think it is a pattern to use the scope outside of the handler to cache things, any objects that are mutated while the lambda remains hot between invocations, can be used as in-memory caches. Are you using: https://github.com/vendia/serverless-express. It may solve your stateful server issues, also that community may know a fair bit about running express in lambda 👍
f
@Andy Averbuch most common uses are things like initializing long lived db or websocket connection, initializing aws client, etc. Anything that’s not mutated between invocations are generally safe to go outside of the handler.
a
Thank you guys! I was worried that the initialization of the serverless express outside of the handler can cause leaks. I’ll tweak accordingly.