Is this expected behavior for individually packing functions?

Hi folks,

I’ve being testing the package feature in multiple scenarios to see which one would fit best our project. We use nodejs based functions and I’d like to create individual packages per-functions, where which function have its own node_modules in its directory, however we have a top level node_modules with common requires that would be added using the package config as state in package dos.

It seems sls does create individual packages however they all contain all functions instead of they’re own. Is this how it was supposed to work?

Thanks,

sls version: 1.0.0-rc-1
serverless.yml
service: aws-nodejs # NOTE: update this with your service name

provider:
  name: aws
  runtime: nodejs4.3
  stage: dev
package:
  individually: true

functions:
  user:
    handler: user/handler.hello

  account:
    handler: account/handler.hello

directory layout
.
├── account
│ ├── event.json
│ ├── handler.js
│ └── package.json
├── package.json
├── serverless.yml
└── user
├── event.json
├── handler.js
└── package.json

sls package1.zip
[eric@gross sls-multi-pkg]$ unzip -l .serverless/aws-nodejs-dev-account.zip
Archive: .serverless/aws-nodejs-dev-account.zip
Length Date Time Name
--------- ---------- ----- ----
63 2016-09-19 12:24 account/event.json
324 2016-09-19 12:24 account/handler.js
254 2016-09-19 12:24 account/package.json
252 2016-09-19 12:24 package.json
63 2016-09-19 12:24 user/event.json
316 2016-09-19 12:24 user/handler.js
246 2016-09-19 12:24 user/package.json
--------- -------
1518 7 files

sls package2.zip
[eric@gross sls-multi-pkg]$ unzip -l .serverless/aws-nodejs-dev-user.zip
Archive: .serverless/aws-nodejs-dev-user.zip
Length Date Time Name
--------- ---------- ----- ----
63 2016-09-19 12:24 account/event.json
324 2016-09-19 12:24 account/handler.js
254 2016-09-19 12:24 account/package.json
252 2016-09-19 12:24 package.json
63 2016-09-19 12:24 user/event.json
316 2016-09-19 12:24 user/handler.js
246 2016-09-19 12:24 user/package.json
--------- -------
1518 7 files

If you want to get the benefit of packaging your functions individually, you’ll need to set up per-functions include/exclude settings in the function definitions e.g. (adapted from the docs):

service: my-service
package:
  individually: true
functions:
  hello:
    handler: handler.hello
    package:
      include:
        - node_modules/
        - hello/
  world:
    handler: handler.hello
    package:
      exclude:
        - node_modules/

so I have to manually define all includes an excludes per function to prevent serverless from deploying the same package for every function??

Yes, at the moment you do if you want to control the size of your function’s deployment.

Serverless supports all the runtimes that Lambda supports, and so currently doesn’t know (at a code level) what your individual functions require to be packaged with them to work - given the different methods of importing and managing dependencies it’s going to be non-trivial problem to solve programatically.

Having the ability to define includes yourself means you can include non-code related assets (e.g. data files if you wish).

1 Like

Hi,

You may exclude all globally and then include what you want in every function.

package:
  individually: true
  exclude:
    - "**/*"  # quotes(") are needed

functions:
  user:
    handler: user/handler.hello
    package:
      include:
        - user/**
        - node_modules/**

  account:
    handler: account/handler.hello
    package:
      include:
        - account/**
        - node_modules/**
2 Likes