Search code examples
node.jsamazon-web-servicesaws-lambdaserverless-framework

How do I reference a function that is not in the root folder using the Serverless Framework?


I'm using the Serverless Framework 1.x and I want to define my serverless.yml to reference a function that is located in another folder (not in the root level).

For example, in the following folder structure, I want to reference a handler() function that is defined in a function1.js file inside the folder functions.

serverless.yml
functions/
  function1.js
  function2.js
  function3.js
package.json
node_modules/
  ..

All examples that I see consider the following basic scenario where the file is in the root:

serverless.yml
handler.js

Where the serverless.yml file is defined by:

functions:
  hello:
    handler: handler.hello

Solution

  • The Serverless Framework access functions inside other folders using the following syntax:

    folder/filename.function
    

    So, if we have a file named function1.js with a function handler() that we want to execute when our Lambda function is invoked, we use the following definition inside the serverless.yml file:

    service: example
    
    functions:
      func1:
        handler: functions/function1.handler
      func2:
        handler: functions/function2.handler
    

    The same would apply for multiple levels of folders:

    folder/folder/folder/filename.function