Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packaging bug when using nodejs based custom runtime #664

Closed
MatteoGioioso opened this issue Dec 10, 2020 · 6 comments · Fixed by #675
Closed

Packaging bug when using nodejs based custom runtime #664

MatteoGioioso opened this issue Dec 10, 2020 · 6 comments · Fixed by #675

Comments

@MatteoGioioso
Copy link
Contributor

MatteoGioioso commented Dec 10, 2020

This is a Bug

Description

I am trying to have a custom runtime for one of my lambda function. However when I specify runtime: provided webpack is not packaging my function as expected. Instead is packaging the whole project including all the other functions.
If my runtime is not provided (just standard nodejs) then the packaging is correct.
Without webpack plugin it works fine as well.

I have a repro case here: https://github.com/MatteoGioioso/serverless-webpack-bug

Additional Data

  • Serverless-Webpack Version you're using: 5.3.4
  • Webpack version you're using: 4.29.6
  • Serverless Framework Version you're using:
Framework Core: 1.77.1
Plugin: 3.6.18
SDK: 2.3.1
Components: 2.33.2

Webpack configuration


const path = require('path');
const nodeExternals = require('webpack-node-externals');
const slsw = require('serverless-webpack');

module.exports = {
  entry: slsw.lib.entries,
  target: 'node',
  devtool: 'source-map',

  externals: [nodeExternals()],
  module: {
    rules: [
      {
        test: /\.js$/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env'],
          },
        },
        include: __dirname,
        exclude: /node_modules/,
      },
    ],
  },
  output: {
    libraryTarget: 'commonjs2',
    path: path.join(__dirname, '.webpack'),
    filename: '[name].js',
    sourceMapFilename: '[file].map',
  },
};

Serverless.yaml simplified

service: sls-webpack-bug

provider:
  name: aws
  runtime: nodejs12.x

custom:
  webpack:
    webpackConfig: ./webpack.config.js

package:
  individually: true

plugins:
  - serverless-webpack

functions:
  exampleFunction:
    memorySize: 1024
    timeout: 30
    handler: services/myService/handler.default
    runtime: provided

  exampleFunction2:
    memorySize: 1024
    timeout: 30
    handler: services/anotherService/myHandler.default

If I have time I will make a repro case, but I think that should be it for the time being.

PS: The custom runtime is nodejs in this case

Thanks

@MatteoGioioso
Copy link
Contributor Author

Ok, I have manage to reproduce it. https://github.com/MatteoGioioso/serverless-webpack-bug
So if runtime: provided is passed than the webpack plugin won't work as expected packaging the whole project

@MatteoGioioso MatteoGioioso changed the title Packaging bug when using runtime provided Packaging bug when using nodejs based custom runtime Dec 11, 2020
@MatteoGioioso
Copy link
Contributor Author

MatteoGioioso commented Dec 11, 2020

Ok, this is not really a bug:

if (runtime.match(/node/)) {

You just do not allow any other runtime other than node, and this is perfectly understandable, however in my case I am using a custom runtime which is still nodejs base.

So this will turn into a question. How can I include in the serverless-webpack plugin my function with custom runtime?

  • I have tried custom entries but with package individually: true it will throw an error
  • Forking the repo and comment out that line of code worked.

Sorry for the monologue =)

@MatteoGioioso
Copy link
Contributor Author

Can we have this as a feature? If there is no other workaround of course.

This is the proposed change, not sure you will like it or not.

We add an additional property to allow a custom runtime only if the user set this to true

  exampleFunction:
    memorySize: 1024
    timeout: 30
    handler: services/myService/handler.default
    runtime: provided
    webpackAllowCustomRuntime: true // whatever name you guys want

And then add another check:

if (runtime === 'provided' && loadedFunc.webpackAllowCustomRuntime) {
        const entry = getEntryForFunction.call(this, functions[index], loadedFunc);
        _.merge(entries, entry);
}

Let me know what you think, on my fork this is already done and tested.

@j0k3r
Copy link
Member

j0k3r commented Dec 11, 2020

Maybe allowCustomRuntime is enough. That might be a good addition.

We'll also need to write that down in the doc that you know what you are doing when using that option.

@MatteoGioioso
Copy link
Contributor Author

@j0k3r ok, I can update the docs as well in the same PR if you guys need

@MatteoGioioso
Copy link
Contributor Author

@j0k3r ok, I have made a PR for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants