serverless-webpack icon indicating copy to clipboard operation
serverless-webpack copied to clipboard

Omit Lambda Layers from being bundled

Open JustinPlute opened this issue 5 years ago • 9 comments

How can you omit Lambda layers from being bundled in your Lambda functions? Is this supported?

import { myFunc } from '/opt/my-layer'

Related issue, but the author closed it without saying why: https://github.com/serverless-heaven/serverless-webpack/issues/487

JustinPlute avatar Aug 31 '19 23:08 JustinPlute

I tried doing so by using the magic comments from Webpack and dynamic import but for some reason it fails. It seems that the magic comments are totally ignored.

taktakpeops avatar Oct 21 '19 06:10 taktakpeops

I actually found a way to get my webpack config to work. My issue was in the externals configuration.

In my code, I am doing the following:

async function handler() {
  const dep = await import('/opt/someDep');
}

In my webpack.config.js, I added the following:

externals: [
  { 'aws-sdk': 'commonjs aws-sdk' },
  { '/opt/someDep': 'commonjs /opt/someDep' },
],

The last line in the externals config does the required magic to get the function bundled and deployed :)

taktakpeops avatar Oct 21 '19 14:10 taktakpeops

On my side, i added it as a dev dependencies in my package.json, then use the forceExclude parameter (https://github.com/serverless-heaven/serverless-webpack#aws-sdk) :

# serverless.yml
custom:
  webpack:
    includeModules:
      forceExclude:
        - moment

And add it in externals in my webpack config as previous message :

// webpack.config.js
externals: [ "moment"]

Seems to be working on my side

monisnap-julien avatar Mar 18 '20 14:03 monisnap-julien

I am also looking for the solution to create or update Lambda Layer based on extracted packages instead of adding to the target package file (.zip file of the handler). If we can do that, we can increase the build time a lot as the dependencies are managed and uploaded by Layer not the handler. The cold starts issue also can be reduced. Right now our cold start is around 1.5s for a handler with package size is around 10mb.

Does anyone have any approach?

hoang-innomize avatar Mar 31 '20 04:03 hoang-innomize

Hello you can do something like this

// eslint-disable-next-line no-undef
const {global} = __non_webpack_require__("/opt/config");

alessaloisio avatar Apr 01 '20 07:04 alessaloisio

@alessaloisio This just the way to require/import package. We need to create layer and deploy automatically based on required package.

For example, when we build package for a service it extracted and packaged the outlined packages and add to the handler. But instead of adding to handler, I want to create a layer (automatically based on this plugin). So the result of sls deploy will have Lambdas and also required layer (node packages only).

Serverless: Bundling with Webpack...
   67 modules
Serverless: Package lock found - Using locked versions
Serverless: Packing external modules: [email protected], @nestjs/[email protected], @nestjs/[email protected], [email protected], [email protected], @nestjs/[email protected], [email protected], @sentry/[email protected], [email protected], [email protected], [email protected]
Serverless: Packaging service...

hoang-innomize avatar Apr 01 '20 07:04 hoang-innomize

@hoang-innomizetech I was answering the basic question how to make webpack ignore the requires coming from our layers. For you, I think you can look into this module, it automatically separates dependencies into layers. serverless-layers

PS: I am also looking for a solution to this problem with webpack, I give you a feedback if I succeeded.

alessaloisio avatar Apr 01 '20 07:04 alessaloisio

module.exports = {
  //...
  externals: [nodeExternals(), /^\/opt/, 'aws-sdk'] // exclude Lambda Layers
  //...
};

alexbondify avatar Apr 05 '21 15:04 alexbondify

what about linked modules? such as linked module by $ yarn link any_module

harleyguru avatar Jun 29 '21 22:06 harleyguru