https://github.com/finndersen/aws_sam_custom_build_method
A basic project template for using a custom AWS SAM build method so that each Lambda only contains the source code it needs.
https://github.com/finndersen/aws_sam_custom_build_method
Last synced: 11 months ago
JSON representation
A basic project template for using a custom AWS SAM build method so that each Lambda only contains the source code it needs.
- Host: GitHub
- URL: https://github.com/finndersen/aws_sam_custom_build_method
- Owner: Finndersen
- Created: 2025-01-28T14:25:51.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-01-28T15:35:51.000Z (over 1 year ago)
- Last Synced: 2025-06-07T19:46:33.944Z (about 1 year ago)
- Language: Shell
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Custom SAM Build Method for Multi-lambda Monorepo
A basic project template to demonstrate using a custom AWS SAM build method so that each Lambda only contain the source code it needs.
## Issue with Standard build method
In you have a project involving multiple Lambda functions with shared code, having a structure like:
```
my-project/
├── src/
│ ├── function_a/
│ ├── function_b/
│ └── shared/
├── tests/
├── requirements.txt
└── sam-template.yaml
```
And with a SAM resource configuration like:
```yaml
Resources:
LambdaA:
Type: AWS::Serverless::Function
Properties:
Handler: function_a.app.lambda_handler
Runtime: python3.11
CodeUri: src/
...
```
The standard AWS SAM build method will package all the source code into each Lambda, meaning each Lambda will contain code it does not need, and will have a new version published even when its own relevant code has not changed.
Example build output:
```
build/
├── LambdaA/
│ ├── function_a/
│ ├── function_b/ # This is not needed
│ ├── shared/
│ └──
├── LambdaB/
│ ├── function_a/ # This is not needed
│ ├── function_b/
│ ├── shared/
│ └──
```
## Solution
AWS SAM supports defining [custom build logic](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/building-custom-runtimes.html) using Makefile commands.
This project includes an example custom build process which only includes the necessary source code in each Lambda, so the build output will look like:
```
build/
├── LambdaA/
│ ├── function_a/
│ ├── shared/
│ └──
├── LambdaB/
│ ├── function_b/
│ ├── shared/
│ └──
```
## Usage
To run default build method:
```bash
sam build --template sam-template-basic.yaml --manifest requirements.txt
```
To run custom build method:
```bash
sam build --template sam-template-custom.yaml
```
## Limitations
This setup involves a single set of dependencies for all Lambdas, so this may result in a Lambda being built with dependencies it does not need.
The dependencies are downloaded only once for efficiency and copied for each Lambda so this may not be a big deal, but it should also be possible to extend this solution to support Lambda-specific dependencies if required.