question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

package: individually: true generates huge files when using the same handler file for multiple functions

See original GitHub issue

This is a Bug Report

Description

For bug reports:

  • What went wrong? I ran serverless deploy -s test and checked the code size of the functions in the lambda console. The size had gone up from 326.9 kB to 36.4MB.
  • What did you expect should have happened? The size to go down, and differ between the packages
  • What was the config you used? serverless.yml
package:
  individually: true
custom:
  webpackIncludeModules: true

webpack.config.js

const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
const slsw = require('serverless-webpack')
const Visualizer = require('webpack-visualizer-plugin')

module.exports = {
  entry: slsw.lib.entries,
  target: 'node',
  externals: ['aws-sdk'],
  plugins: [
    new UglifyJSPlugin(),
    new Visualizer({filename: '../webpack-stats.html'}),
  ]
}
  • What stacktrace or error message from your provider did you see?

Additional Data

  • Serverless-Webpack Version you’re using: github master (git://github.com/elastic-coders/serverless-webpack.git#85560afce2c60cdf6c4ba54392e45e991e0cc595)
  • Webpack version you’re using: 3.5.5
  • Serverless Framework Version you’re using: 1.20.2
  • Operating System: Debian Stretch
  • Stack Trace (if available):

My serverless.yml has 3 functions, all in the same file, handler.js.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
HyperBraincommented, Aug 21, 2017

It is fixed in the attached PR, but consider changing the project layout as mentioned above 😉

2reactions
HyperBraincommented, Aug 21, 2017

@msl-kabo I will fix the wrong packaging issue, but I see a general flaw in the layout of the project. Webpack can only set whole files as entries (see here). So, if all of your handlers are exports of the same file, this file will be optimized (with all exports that it has in it). This is not optimizable on a function level though.

The benefit from Webpacks optimization is only usable, if you separate the handler layer from the business logic, i.e. you should create 3 handlers for the functions that call the specific export from the lib files. Then Webpack will analyze each handler separately and only use the code used by each specific handler that is accessed from the handlers.

Sample:

# serverless.yml
functions:
  funcA:
    handler: handlers/funcA.handler
  funcB:
    handler: handlers/funcB.handler
  funcC:
    handler: handlers/funcC.handler

File system:

  handlers/
    funcA.js
    funcB.js
    funcC.js
  lib/
    handler.js

The handlers now export a handler function and each handler calls into lib/handler.functionX. Now Webpack can optimize the whole code correctly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Packaging - Serverless Framework
To enable individual packaging set individually to true in the service or function wide packaging settings. Then for every function you can use...
Read more >
Deploy multiple functions using serverless creating each ...
Hi, What I am suggesting is separate lambda functions/entry points only. Each lambda function is is separate .java file. In my case, I...
Read more >
Breaking a large serverless.yml into manageable chunks
Breaking the file. Start by creating 2 new serverless.yml files in the function sub folders randomGenerator and uuidGenerator. And copy the ...
Read more >
Azure Functions best practices | Microsoft Learn
To reduce latency, create the storage account in the same region as the function app. To improve performance in production, use a separate ......
Read more >
Best practices for organizing larger serverless applications
Many serverless applications begin as monolithic applications. ... Using these packages in Lambda functions results in architectures like ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found