This example demonstrates how to build clean, testable, and modular serverless applications on AWS using AWS SAM, Lambda Layers, and Jest. It focuses on two essential practices for scalable serverless development:
- Using Lambda Layers to share code across functions
- Using Jest to properly test each function and its dependencies
By combining these two patterns, you get a powerful structure that’s maintainable, reusable, and confidently testable.
Project Overview: Shared Logic + Layer for Node Modules + Testing
-
functions/– Your Lambda function handlers:foo.mjs– A function that fetches data from an external API.bar.mjs– A function that uses a helper function (sayHello()).libs/shared.mjs– Shared helper module used by both functions, kept in the localfunctions/libs/folder.
-
layers/nodejs/– The Lambda Layer:- Includes
package.jsonandpackage-lock.json. - This layer is used to provide shared dependencies (like
axios) to your functions without bundling them in each deployment package.
- Includes
-
tests/– Dedicated Jest tests for each function:foo.test.mjsbar.test.mjs
-
jest.config.mjs– Configures Jest for testing ESM-based Lambda functions and shared code.
Project Repository: https://github.com/OmarMakled/sam-test-jest