Repository navigation
Open
Labels
Description
Currently packaging is really slow, and broken when symlinks are involved (they're not recognized as links, and in case of circular references packaging process just hangs)
It most likely can be attributed to inefficient glob patterns handling, which also takes place at excluding dev dependencies step that takes an unjustified amount of time.
Fix would be to use fastest possible file system traversal (no glob involved when possible), which on the go ignores dev dependency paths (by default, but with opt-out possibility) and recognizes symlinks (and other custom type of files as FIFO -> #6602) as expected. We should also look here into work done at serverless-jetpack as afaik it followed similar route.
Additional improvements to consider:
- Provide a way to customize packaging logic per provider/runtime, both for internal needs and for plugins
- (Node.js only) Package only modules used by given lambda (detected via require paths traversal). This is currently offered by external serverless-plugin-reducer plugin. It'll be good to have such option incorporated into core
- (Node.js only) Support out of a box transpilation of projects that rely on babel or typescript. Still that should be achieved without registering babel or typescript as serverless dependencies. It's serverless projects that should list them in dependencies sections and having that we may offer automation.
- (Node.js only) (assuming it's proven as worthwhile one) Support bundling into one file - simple solution. dedicated for Node.js projects, which typically wouldn't require any extra configuration (do not involve Webpack which covers hundreds of browser specific cases)
Reacted by Samuel Chan, Furkan Tunalı, Mariusz Nowak, Theo, Kyle McNally, Roman Safronov, Chema Lopez, collinalexbell, Marcelo Rodriguez, Torsten Rüter and 21 more
Reacted by Sergii Kovalov
Activity
[-]Improve packaging of Node.js lambdas[/-][+]Improve packaging of lambdas[/+]jleclanche commentedon Oct 28, 2019
Note from #6890, current implementation looks at everything under
excludedespite never packaging it. These should be skipped earlier on if we want to improve performance as well (especially since by nature exclude may have some very large amounts of non-packageable files).thejuan commentedon Feb 25, 2020
It'd be great if this can also look at allowing packaging per runtime for polyglot projects.
medikoo commentedon Apr 27, 2020
A valuable insight by @ryan-roemer (co-author of serverless-jetpack) that we should take into account: #6007 (comment)
ryan-roemer commentedon Apr 27, 2020
Hi @medikoo! Thanks for bringing over that comment here.
Reading this issue and seeing the mention a different packaging approach of your lib
serverless-plugin-reducer, it's probably worth nothing that Jetpack has recently introduced a similar means of "trace only the files used by a handler" incustom.jetpack.tracing.OPTIONS. Some references:The tracing isn't too hard, so the things we're working on now (in a branch I'll publish soon are);
require(A_VARIABLE)orimport(A_VARIABLE)) programmaticallyThe extras we already have in the feature are:
try { require("an_optional_mod"); } catch (err) {}jetpack.trace.ignore = ["aws-sdk"]and prevent tracing of anyrequire("aws-sdk")meaning you skip not only the core lib but also transitive dependencies.The underlying library that does most of the tracing work for us is: https://github.com/FormidableLabs/trace-deps
And the speedups we're seeing in a current client project that we're in the middle of integrating Jetpack's tracing mode into is pretty staggering (this is a single Serverless function that just ballooned enormously because of over-specified prod dependencies).
At any rate, hope that helps!
medikoo commentedon Apr 27, 2020
Great thanks @ryan-roemer for detailed feedback! We'll definitely take that into account
27 remaining items
scadu commentedon Dec 7, 2020
@medikoo, I just wanted to confirm: were any changes introduced already or this task is waiting in a queue?
Thanks in advance for clarifying :).
BryanCrotaz commentedon Dec 7, 2020
And note I've already completed the work and I'm using my fork in production.
medikoo commentedon Dec 7, 2020
@scadu thanks for asking. Most likely this month we will cover this: #8586
Still other tasks in scope of those issue were pushed further in a queue (behind #6580), so unfortunately it'll take more time to sort this out
PatrykMilewski commentedon Dec 11, 2020
Is this a good way to achieve this? There is a new tool ESBuild which is a lot faster and better than Webpack and there is already a plugin that works with it:
https://github.com/floydspace/serverless-esbuild
I believe it would achieve the same goal. Time for packaging stack with multiple Lambdas was reduced for me from around 50 seconds to less than once second. Maybe let's try to bundle ESBuild tool into Serverless? It would solve all of the problems with:
Since ESBuild is still quite a new kid in the town, it could be an experimental feature at the beginning.
medikoo commentedon Dec 11, 2020
@PatrykMilewski thanks for comment. Yes we have our eyes on ESBuild.
Ideally we'd like to ensure an a fast, reliable, configuration less, packaging, that also produces easy to debug lambdas, and having that in mind our approach is as follows:
A. In first stance, we want to ensure fast packaging of lambdas where whole service content (with exclusion of
devDependencies) is made. This ensures that users doesn't have to work on any configuration. Things should just work, and files are put as is to lambda, so in terms of debugging any stack traces are clean.Note: That part should be addressed with #8586
But, obviously for some cases it may not be enough, in more complex projects produced lambda packages could end really large affecting deploy performance, to address that next option would be introduced:
B. Traverse require/import paths, for each lambdas individually and package only files that are used by lambda.
This will reduce lambda package size significantly, and leave easiness of debug at same level, still this may require some extra configuration effort, if lambda files rely on dynamic requires, or some other static files which need to be bundled with package, it will have to be outlined in configuration.
For cases where above may still not yet turn performant enough, we may introduce next option
C. Bundle lambda into single file (with tools as ESBuild). This may result with even smaller lambda package, but again is likely to require extra configuration options, and easiness of debugging issues will certainly be affected.
Additionally the more complex toolset and logic we use for packaging, the bigger chance for bugs and quirks (in used tools and sophiosticated logic) user will have to deal with
yeukfei02 commentedon Jan 21, 2021
I have a problem of using
sls package.when I use
this will not use something like
webpackto package individual lambda func into zipfile.Is it possible to use something like
serverless-webpackorwebpackto bundle individual lambda func into zipfile.there may be an option in
serverless.ymlto enable/allow developer to bundle whole project/individual lambda func (with webpack way to bundle)I know this maybe take time, but just give the suggestion/idea that I encounter.
Thanks.
medikoo commentedon Jan 21, 2021
@yeukfei02 please ask at
serverless-webpackrepository. We do not handle webpack bundling hereBryanCrotaz commentedon Jan 21, 2021
o-alexandrov commentedon Feb 6, 2021
@medikoo
You should update the issue description, since webpack 5 doesn't polyfill node.js at all:
Also, if you truly consider
esbuild, it has a lot of uncovered failing edge cases (just go through releases and you'll see a lot of bugs being fixed, that shouldn't exist in a stable tool):And finally, on the performance of the actual builds & esm support, webpack is going to soon almost match
esbuildin performance as there are plans to skip proceessing of esm modules (which would feature almost the same speed toesbuild, but again you cannot compare a tool (esbuild) that has much less features to webpack.In most cases, when talking about bundling
AWS Lambda for Node.js, we typically talk about code that is shared between frontend and backend.Therefore, tools like
esbuildshouldn't be even considered, as it's just not ready to handle all possible cases for both the web & server-side.Whereas using plain
tsc(TS) compilation cannot be considered astscdoesn't support minification and cannot be compared to babel ecosystem, as babel plugins can tremendously shred the resulting module (compile time minifications/transformations for example for graphql, prepacking code that could be analyzed before runtime, etc)For now, since
webpackdoesn't support ESM output, reasonable solutions when packaging lambda functions individually are:babel+terser(babel-minify is worse than terser for minification)serverless' "functions"configuration, just loop through the list and programmatically compile in parallel usingbabel + terser, as simple as thatesbuildrollup(as a stable tool) +babel+terserjlarmstrongiv commentedon Sep 22, 2021
I gave up long ago and switched to Docker images for packaging https://www.serverless.com/blog/container-support-for-lambda much clunkier, but always works
jweyrich commentedon Oct 15, 2021
We adopted the serverless-jetpack last year and the speed gains are huge. We have over 800 Lambdas in each environment. If you want some input regarding that, I can provide more details. Due to NDA I cannot publish them here, but I can get and authorization to share the results privately.
vicary commentedon Sep 6, 2022
@jweyrich I just see your last comment in my casual checking, and I am really interested in your bundling time. We have projects with ~300 Lambdas and 45 mins - 60 mins of bundling time is the best we can do with either
serverless-webpackandserverless-esbuild.I believe your numbers would definitely enhance the context of this issue.
BryanCrotaz commentedon Sep 6, 2022
mrbrianevans commentedon Aug 14, 2023
Would it help if the lambda packaging was done in a worker thread? I'm assuming its single threaded in the main thread because while packaging my cli freezes up and task manager shows only one core being utilised.
See nodejs worker_threads.