Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/oieduardorabelo/aws-sam-nested-applications-single-package-json
AWS SAM Nested Applications with single package.json to all lambda handlers
https://github.com/oieduardorabelo/aws-sam-nested-applications-single-package-json
amazon-api-gateway aws aws-lambda aws-sam aws-sam-cli aws-sam-example aws-sam-nested-applications serverless typescript
Last synced: 10 days ago
JSON representation
AWS SAM Nested Applications with single package.json to all lambda handlers
- Host: GitHub
- URL: https://github.com/oieduardorabelo/aws-sam-nested-applications-single-package-json
- Owner: oieduardorabelo
- Created: 2023-03-08T05:47:52.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-03-08T08:27:37.000Z (over 1 year ago)
- Last Synced: 2024-04-14T19:10:11.169Z (7 months ago)
- Topics: amazon-api-gateway, aws, aws-lambda, aws-sam, aws-sam-cli, aws-sam-example, aws-sam-nested-applications, serverless, typescript
- Language: TypeScript
- Homepage:
- Size: 195 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# AWS SAM Nested Applications with single package.json
This is an example of using AWS SAM with nested applications sharing a single `package.json`.
We have a Rest API TypeScript application (`apps/rest-api/handlers`) that uses `dependencies` from the root `package.json`.
We have a DynamoDB Stream with a TypeScript lambda consumer (`apps/dynamodb/handlers`) that uses `dependencies` from the root `package.json`.
Any application can define its own TypeScript Lambda and use the root `package.json`.
The parent AWS SAM application is defined in `./template.yaml` and child applications are defined in `./apps/*/template.yaml`.
## Benefits
This enables the developer to use a single `package.json` and share it across the entire application. `esbuild` will be packing each lambda handler bundling the correct dependencies imported by each handler.
## Drawback
You need to define `--base-dir` for every AWS SAM CLI command that have it as an option.
When using `sam sync` chaging the source code of one lambda with sync all lambdas of that stack. For example, `apps/rest-api` is a stack with 2 lambdas. If you change the source code of one lambda, all 2 lambdas will be synced.
## Conventions on how to use
We have a few conventions:
1. The `package.json` is located in the root of the project
2. The `CodeUri` of any lambda is always `./`
3. Definitions for lambda handlers entry points are defined using the full relative path of the project root
4. When running `sam build` from the root of the project, we need to define `--base-dir .` to make sure that `esbuild` will be able to find the `package.json` in the root of the project and resolve all relative paths correctlyFor example:
```yaml
Resources:
GetBooksFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./
Handler: get-books.handler
# ...
Metadata:
# ...
BuildProperties:
# ...
EntryPoints:
- apps/rest-api/handlers/get-books.ts
```**Important:** The `--base-dir` must be defined for every AWS SAM CLI command that have it as an option.
## Deploying to AWS
You can use any AWS SAM CLI command, make sure you include `--base-dir .` when running from the root of the project.
With `sync`:
```bash
sam sync --base-dir .
```With `deploy`:
```
sam build --base-dir .
sam deploy
```## Running locally
You can run it locally as you would normally do with AWS SAM CLI.
With `local start-api`:
```bash
sam build --base-dir .
sam local start-api
```With `local invoke`:
```bash
sam build --base-dir .
sam local invoke StackRestApi/GetCategoriesFunction
```