Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serverless framework. Exclude not needed functions from package

My service structure:

-MyService
    -common
    -node_modules
    -functions_folder
        -Function1.js
        -Function2.js
        -Function3.js

yaml file:

service: MyService

provider:
  name: aws
  runtime: nodejs6.10
  stage: dev

functions:
    Function1:
       handler: functions_folder/Function1.handler
       memorySize: 512
       timeout: 10

    Function2:
       handler: functions_folder/Function2.handler
       memorySize: 512
       timeout: 10

     Function2:
       handler: functions_folder/Function3.handler
       memorySize: 512
       timeout: 10

When i’m deploying, i have 3 different lambda functions, but each one contain Function1.js, Function2.js, Function3.js inside.

Can somebody explain me please how to exclude from resulted Lambda not needed files?

like image 221
Yevhen Avatar asked Jan 29 '18 08:01

Yevhen


People also ask

Why you should not use serverless?

Loss Of Control‍ One of the biggest disadvantages of serverless is that you don't have the control over your services. We use a lot of services that are managed by third-party cloud providers, like Cloudwatch for logs and DynamoDB for databases.

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 do I pass an environment variable in serverless?

To reference environment variables, use the ${env:SOME_VAR} syntax in your serverless. yml configuration file. It is valid to use the empty string in place of SOME_VAR . This looks like " ${env:} " and the result of declaring this in your serverless.


1 Answers

After some time researching i found solution. So here it is:

service: MyService

package:
  individually: true
  exclude:
    - ./**
  include:
    - common/**
    - node_modules/**

provider:
  name: aws
  runtime: nodejs6.10
  stage: dev
  memorySize: 512
  timeout: 10

functions:
    Function1:
       handler: functions_folder/Function1.handler
       package:
         include:
           - functions_folder/Function1.js

    Function2:
       handler: functions_folder/Function2.handler
       package:
         include:
           - functions_folder/Function2.js

     Function2:
       handler: functions_folder/Function3.handler
       package:
         include:
           - functions_folder/Function3.js

So as you can see in package section i have added include/exclude part, at first i'm excluding all files then i'm include 2 needed folders "common" and "node_modules". After this for each function i also use include command for to add only needed file.

like image 159
Yevhen Avatar answered Sep 28 '22 08:09

Yevhen