This article highlights the steps we took to reduce the size of our NodeJs apps running on AWS Lambda but it still relates to any Node app running anywhere. In the end, you'll find out how we dropped our 50MB - 75MB (compressed) node apps to 8MB - 10...
log.victoranuebunwa.com5 min read
Other things to consider, which work great for us at Webiny, are:
1) Use webpack to bundle your handlers. With this you only get production dependencies that you actually use in code without even thinking about it, + everything gets minified, which greatly reduces the handler size. This also provides the benefit of using tools to analyze your webpack bundle to identify large dependencies and potentially get rid of them.
With webpack it's easy to exclude any module (like aws-sdk) from the bundle, using the externals parameter. This brings us to the next point:
2) Extract dependencies you share in multiple handlers into Lambda Layers. Layers can contain both 3rd party npm modules as well as your own project level packages. Once the layer is attached to your Lambda, you import those dependencies as any other node module.
If you use this approach, you don't need to worry about accidentally leaving aws-sdk in the production dependencies, because webpack will exclude it anyway. And you can also exclude all the dependencies that will be provided by the Lambda Layers.
Wow, I had no idea that aws-sdk is so huge! I'll definitely keep that one in my mind next time I'm working with it!
Really helpful! ๐
I use serverless functions a lot and the tips you provided in this article are really helpful!
Some of the tips will be really helpful in other serverless platforms which have a strict runtime of 1s.
Just because to avoid installing unnecessary packages I prefer writing my own functions whenever possible.
dauda cindy
Civil Engineer
Great !!! ๐๐๐