Serverless Functions

The serverless/functions directory contains the code for Lambda functions. The developer first creates the lambda resource in the template.yaml, then adds a typescript file containing code under the functions directory. The lambda resource in the template.yaml must have a reference to the filename of the corresponding function.

Every SOMOD function must have a lambda handler as the default export. The SOMOD prepare command will generate a javascript bundle capable of running inside nodeJs runtime at the .somod/serverless/functions/{moduleName}/{functionName} directory.

SOMOD excludes the npm packages in the attached layers from the function bundle.

By default, the lambda functions are bundled for NodeJs version 16. bundle for a different version of NodeJs by setting SOMOD_SERVERLESS_NODEJS_VERSION environmental variable before calling somod prepare command.


The developer can add common libraries used across multiple functions in a layer. Refer SOMOD::Function and SOMOD::FunctionLayer keywords in serverless/template.yaml for syntax.

Invoking the functions

AWS SAM documentation describes the event sources to configure the invocation of the function.

The lambda function can handle any of the events described in the event sources and more. In a SOMOD Module, each lambda function must handle only one type of event and the same should match the type property of SOMOD::Function keyword in serverless/template.yaml

For HttpApi and Api event sources SOMOD automatically tries to resolve the conflicting API Routes. For conflict resolution strategy, refer to Namespaces.


By default, functions are bundled and minified when prepared. Enable --debug flag with somod prepare command to keep the functions un-minified along with source-map enabled in the deployed lambda function.

Extending the functions

Replace the whole lambda function code in a dependent module by Extending the function resource and proving the new code to SOMOD::Function.


Wrap the function with the middleware using SOMOD's middleware framework.

In the next chapter, let us dive into the concept of middleware in SOMOD.

Does this page need improvements?
Edit This Page in GitHub
Did this page help you?
Provide feedback in the GitHub Discussion Page
Need More help?

Write an email to

This documentation is built using
Developed and Maintained By
Sodaru Technologies
© 2023