How can I exclude nested folders in serverless?

Viewed 568

I am using serverless framework to deploy lambda to AWS. I found there are many nested node_modules folders in my project which takes more than 250MB data. I think they are from third party dependencies who have conflict versions of dependencies. How can I exclude packaging all nested node_modules in serverless.yml?

package:
  exclude:
    - nodejs/node_modules/aws-sdk/**
    - nodejs/node_modules/**/node_modules

I have tried above configure but it doesn't exclude them.

1 Answers

Excluding node_modules folders inside a larger node_modules directory is not the best approach, as this would just end up breaking the encompassing modules involved (in which case they might as well not be there at all).

Your best best is to use serverless-webpack. Webpack includes tree shaking, which will vastly shrink the size of your deployment package.

The above should be enough, but you can optimize further by including the following in your serverless.yml:

package:
  individually: true
  excludeDevDependencies: false

This creates separate deployment packages for each of your lambdas and excludes all development dependencies. (They are unrelated so you could use just one or the other if you so chose.) You only mention a single lambda in your question, but given the size of your node_modules I'm guessing you really have multiple lambdas.

If you do only have one lambda, it might be pretty large, so I'd advise splitting it out into multiple smaller lambdas coordinated by a step function. Serverless handles the deployment of step functions as well, so you can manage all of your resources nicely together in the serverless.yml file.

Related