Skip to content

Improve packaging of lambdas #6580

Open

Description

@medikoo

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)

Activity

  1. self-assigned this
    on Aug 22, 2019
  2. changed the title [-]Improve packaging of Node.js lambdas[/-] [+]Improve packaging of lambdas[/+] on Aug 29, 2019
  3. jleclanche commented on Oct 28, 2019

    @jleclanche

    Note from #6890, current implementation looks at everything under exclude despite 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).

  4. thejuan commented on Feb 25, 2020

    @thejuan

    It'd be great if this can also look at allowing packaging per runtime for polyglot projects.

  5. pinned this issue on Feb 25, 2020
  6. medikoo commented on Apr 27, 2020

    @medikoo
    ContributorAuthor

    A valuable insight by @ryan-roemer (co-author of serverless-jetpack) that we should take into account: #6007 (comment)

  7. ryan-roemer commented on Apr 27, 2020

    @ryan-roemer
    Contributor

    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" in custom.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);

    • Handle dynamic imports (e.g. require(A_VARIABLE) or import(A_VARIABLE)) programmatically
    • Bail on missed dynamic imports

    The extras we already have in the feature are:

    • Ignore missing imports (say a node lib does try { require("an_optional_mod"); } catch (err) {}
    • Expllicit ignores to stop tracing. This is good so that you can do jetpack.trace.ignore = ["aws-sdk"] and prevent tracing of any require("aws-sdk") meaning you skip not only the core lib but also transitive dependencies.
    • Additional "traced" files beyond the automagically inferred handler.
    • Works with CommonJS as well as all presently shipped ESM features (which will become more important now that node14 is released).

    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).

    Current Trace vs.
    Build Time 1653.52s 48.42s -97%
    Size 74M 9M -88%
    Num. Files 51,758 4,731 -91%

    At any rate, hope that helps!

  8. medikoo commented on Apr 27, 2020

    @medikoo
    ContributorAuthor

    Great thanks @ryan-roemer for detailed feedback! We'll definitely take that into account

  9. added
    bug/design
    bug/perf
    and removed
    bug/design
    on Apr 30, 2020
  10. 27 remaining items

  11. scadu commented on Dec 7, 2020

    @scadu
    Contributor

    @medikoo, I just wanted to confirm: were any changes introduced already or this task is waiting in a queue?
    Thanks in advance for clarifying :).

  12. BryanCrotaz commented on Dec 7, 2020

    @BryanCrotaz

    And note I've already completed the work and I'm using my fork in production.

  13. medikoo commented on Dec 7, 2020

    @medikoo
    ContributorAuthor

    @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

  14. PatrykMilewski commented on Dec 11, 2020

    @PatrykMilewski

    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:

    • package size optimisation
    • package compilation
    • additional features like tree-shaking, minimizing the code, source maps etc.

    Since ESBuild is still quite a new kid in the town, it could be an experimental feature at the beginning.

  15. medikoo commented on Dec 11, 2020

    @medikoo
    ContributorAuthor

    @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

  16. yeukfei02 commented on Jan 21, 2021

    @yeukfei02

    I have a problem of using sls package.
    when I use

    package:
      individually: true
    

    this will not use something like webpack to package individual lambda func into zipfile.
    Is it possible to use something like serverless-webpack or webpack to bundle individual lambda func into zipfile.

    there may be an option in serverless.yml to 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.

  17. medikoo commented on Jan 21, 2021

    @medikoo
    ContributorAuthor

    @yeukfei02 please ask at serverless-webpack repository. We do not handle webpack bundling here

  18. BryanCrotaz commented on Jan 21, 2021

    @BryanCrotaz
  19. o-alexandrov commented on Feb 6, 2021

    @o-alexandrov

    @medikoo
    You should update the issue description, since webpack 5 doesn't polyfill node.js at all:

    do not involve Webpack which covers hundreds of browser specific cases)

    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):

    There is a new tool ESBuild which is a lot faster and better ...

    And finally, on the performance of the actual builds & esm support, webpack is going to soon almost match esbuild in performance as there are plans to skip proceessing of esm modules (which would feature almost the same speed to esbuild, 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.

    • introducing a tool that is not popular to bundle the frontend would promote the use of two different bundling processes in the project, which would make the solution more error-prone.

    Therefore, tools like esbuild shouldn'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 as tsc doesn'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 webpack doesn't support ESM output, reasonable solutions when packaging lambda functions individually are:

    • babel + terser (babel-minify is worse than terser for minification)
      • that's as slim as you could get
      • you get the list of functions from the user-defined serverless' "functions" configuration, just loop through the list and programmatically compile in parallel using babel + terser, as simple as that
        • result is much more stable and smaller modules than with esbuild
    • or if more customization & processing is needed, then rollup (as a stable tool) + babel + terser
  20. jlarmstrongiv commented on Sep 22, 2021

    @jlarmstrongiv
    Contributor

    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

  21. jweyrich commented on Oct 15, 2021

    @jweyrich

    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.

  22. vicary commented on Sep 6, 2022

    @vicary
    Contributor

    @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-webpack and serverless-esbuild.

    I believe your numbers would definitely enhance the context of this issue.

  23. BryanCrotaz commented on Sep 6, 2022

    @BryanCrotaz
  24. mrbrianevans commented on Aug 14, 2023

    @mrbrianevans

    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.

  25. unpinned this issue on Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug/perfcat/packaging

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions