I took the aws-serverless-express package and deploy it with a DynamoDB GET route to Lambda/API Gateway. When I run my cold-starts I see it taking on average 2,800ms. If I just create a new NodeJS Lambda and deploy it with SAM with the same DynamoDB GET code I see a cold start time of 1080ms on average.
Has anyone else seen that big of a gap with the serverless-express package? I’m seriously considering removing Express all together in order to get those cold-start times lower - I’m just not sure if it’s actually an Express issue or if it’s just related to the number of dependencies in general that come with it.
Do the number of dependencies matter in a Lambda deployment?
Related discussions:
Just guessing here but if you have more dependencies it should simply take longer to load. And then those dependencies should have their own dependencies.
Can you do any minifying/bundling to reduce boot time or do I have to ship the node_modules as-is?
Is it timeouts? Or just cold start. I’d make sure you aren’t initializing the server in the handler code, and then see if its closing th event loop.
Should be <200ms
I don’t use SAM specifically, but bundling with webpack/rollup with tree shaking might help. Also, keeping enough concurrent lambdas warm to handle spikes.
aws-serverless-express cold starts are slow because it’s actually creating an Express server in a separate process, then proxying the request to that server when a request comes in. Starting up that server takes some time.
A different Express-compatible approach in Lambda is to use the serverless-http library. Rather than starting a server, it just assembles the API Gateway event into a shape that is expected by Express, then calls the Express methods directly.
Walkthrough of how to use it here: Deploy a REST API using Serverless, Express, and Node.js (Disclosure: I wrote the article).
Awesome! I’ll read this and work on refactoring my code.
I still saw a 3 second startup time even after moving to serverless-http. Moving from 128mb to 256mb dropped it to 1,000ms again. Looks like I just need to give it a bit more juice.
Either-way, thanks for the link - I’ll keep using this. The Serverless framework seems pretty straight forward. The local debugging of the API Gateway with it is nice too. The SAM model with local debugging always felt convoluted and confusing, with documentation spread between AWS docs and Github.