Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit d19901a

Browse files
authored
docs: improve handler section
1 parent f826ebe commit d19901a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

CONTRIBUTING.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ For interactive debugging of the deployment you may launch serverless through no
6363

6464
Note: If you are working with a Typescript package make sure you build it (`yarn build`) before deploying ;)
6565

66-
### Adding new dependencies
66+
### Updating handler code
6767

68-
If you would like to add new package dependencies, please be mindful of increasing cold start times and/or handler size. Note that JS `require` time has the most impact on cold start times. While code size is also important, it has little effect on cold start times, because Lambda seems to cache the code pretty efficiently.
68+
If you are updating the handler code, please be mindful of increasing cold start times and/or handler size, especially when adding new dependencies or doing complex operations. Note that JS `require` time has the most impact on cold start times. While code size is also important, it has little effect on cold start times, because Lambda seems to cache the code pretty efficiently - and this can be mitigated by minifying the Next.js build.
69+
70+
Note that Node.js seems to have a minimum cold start time of ~150 ms, even on an hello-world handler. So the idea is to try to optimize everything else as much as possible.
6971

7072
For example, importing the built-in AWS SDK JS v2 at the top of `default-handler.ts` (outside of the handler) via the below:
7173

@@ -79,11 +81,11 @@ or even just the S3 client:
7981
import S3 from "aws-sdk/clients/s3";
8082
```
8183

82-
could incur **100 ms** or more cold start times on every handler invocation, even when it's not needed. This is because even though `aws-sdk` (AWS SDK JS v2) is built into AWS Lambda's Node.js runtime, it is not modularized and will `require` a bunch of unused code. Even if using just the S3 client, it also takes close to 100 ms. In traditional server-based environments, we do not have to worry about this, but since Lambda is a serverless environment, containers will get re-initialized and this becomes a performance problem.
84+
could incur **100 ms** or more cold start times on every handler invocation, even when it's not actually used. Also, although the `aws-sdk` (AWS SDK JS v2) is built into AWS Lambda's Node.js runtime, it is not modularized and will `require` a bunch of unused code. Even if importing just the S3 client, it also takes close to 100 ms because it imports code for all S3 operations, even if you use only one. In traditional server-based environments, we do not have to worry about this as it is a one-time cost, but since Lambda is a serverless environment, containers will get re-initialized and this becomes a performance problem.
8385

8486
See issue here for more information: https://github.com/serverless-nextjs/serverless-next.js/issues/580
8587

86-
Instead, consider dynamically importing only when needed. For example, for S3:
88+
Instead, consider dynamically importing only when needed. For example, we use the AWS SDK JS v3 client which for S3:
8789

8890
```ts
8991
const { S3Client } = await import("@aws-sdk/client-s3/S3Client");

0 commit comments

Comments
 (0)