Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include exclude packages in serverless yml from same folder

I have a node_modules folder from there i want to exclude only few modules but other modules should be added while packaging. How can i do that.Also how to exclude some modules using regex. like in my node_modules I have created custom modules that starts with md-request, md-models etc. so i want to exclude only these modules from packaging. Thanks in advance.

like image 510
Vaibhav Patil Avatar asked Mar 04 '19 09:03

Vaibhav Patil


People also ask

How do I refer to a property in Serverless Yaml file?

To reference properties in other YAML files use the ${file(./myFile. yml):someProperty} syntax in your serverless. yml configuration file. To reference properties in other JSON files use the ${file(./myFile.

What does SLS package do?

The sls package command packages your entire infrastructure into the . serverless directory by default and make it ready for deployment. You can specify another packaging directory by passing the --package option.

How does a Serverless package work?

Serverless will auto-detect and exclude development dependencies based on the runtime your service is using. This ensures that only the production relevant packages and modules are included in your zip file.


1 Answers

It's most likely you're trying to exclude dev-dependencies. Install your dev dependencies using npm i some-dependency --save-dev and, by default, Serverless will not pack them into the final artifact.

If that's not sufficient, you can exclude everything and include only what you need.

You can see how Serverless behaves when packaging an artifact as well as seeing how to exclude/include dependencies on the official docs

Here's an example (extracted from their docs) to exclude node_modules but to keep node_modules/node-fetch (which is pretty much what you're looking for):

package:
  exclude:
    - node_modules/**
    - '!node_modules/node-fetch/**'

And this is how you can include only what you need:

package:
  exclude:
    - src/**
  include:
    - src/function/handler.js

EDIT: after the OP's comment, here's how to handle the desired behaviour:

package:
  exclude:
    - node_modules/my_module_1/**
    - node_modules/my_module_2/**
    - node_modules/my_module_3/**
    - node_modules/my_module_4/**

If the module names are very close to each other like the above, you can use the wildcard * to exclude them all in a one-liner:

package:
  exclude: 
    - node_modules/my_module_*/**

EDIT 2: Here's a working example (see that I am excluding aws-sdk):

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

Since the package is too large and I am not able to see it through AWS Lambda's console, I am attaching two screenshots - one with exclude and one without exclude (see how the package size changes).

With exclude:

enter image description here

Without exclude:

enter image description here

This way you don't even need to add the '!node_modules/whatever-i-want-to-keep' argument

EDIT 3: Using the * wildcard in a sample project with manually created node_modules and respective my_module_* directories

enter image description here

enter image description here

like image 80
Thales Minussi Avatar answered Nov 15 '22 02:11

Thales Minussi