Skip to content

Commit d68d9ee

Browse files
authored
Cold start optimization (#174)
Use https://www.npmjs.com/package/lambda-warmer with EventBridge schedule to warm lambda with some concurrency.
1 parent cfeb035 commit d68d9ee

File tree

5 files changed

+996
-5
lines changed

5 files changed

+996
-5
lines changed

cloudformation/iam.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ Resources:
143143
- lambda.amazonaws.com
144144

145145
Policies:
146+
- PolicyName: lambda-self-invoke
147+
PolicyDocument:
148+
Version: "2012-10-17"
149+
Statement:
150+
- Action:
151+
- lambda:invokeFunction
152+
Effect: Allow
153+
Resource:
154+
- Fn::Sub: "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:${LambdaFunctionName}:*"
146155
- PolicyName: lambda-generic
147156
PolicyDocument:
148157
Version: "2012-10-17"

cloudformation/main.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ Resources:
192192
RestApiId: !Ref AppApiGateway
193193
Path: /{proxy+}
194194
Method: ANY
195+
WarmingSchedule:
196+
Type: Schedule
197+
Properties:
198+
Schedule: !If [
199+
ShouldAttachVpc,
200+
'rate(15 minutes)',
201+
'rate(5 minutes)'
202+
]
203+
Input: '{ "warmer":true,"concurrency":3 }'
204+
195205

196206
AppSqsLambdaFunction:
197207
Type: AWS::Serverless::Function

src/api/lambda.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import "zod-openapi/extend";
22
import awsLambdaFastify from "@fastify/aws-lambda";
33
import init from "./index.js";
4+
import warmer from "lambda-warmer";
5+
import { type APIGatewayEvent, type Context } from "aws-lambda";
46

57
const app = await init();
6-
const handler = awsLambdaFastify(app, {
8+
const realHandler = awsLambdaFastify(app, {
79
decorateRequest: false,
810
serializeLambdaArguments: true,
911
callbackWaitsForEmptyEventLoop: false,
1012
});
13+
const handler = async (event: APIGatewayEvent, context: Context) => {
14+
// if a warming event
15+
if (await warmer(event, { correlationId: context.awsRequestId }, context)) {
16+
return "warmed";
17+
}
18+
// else proceed with handler logic
19+
return realHandler(event, context);
20+
};
21+
1122
await app.ready(); // needs to be placed after awsLambdaFastify call because of the decoration: https://github.com/fastify/aws-lambda-fastify/blob/master/index.js#L9
1223
export { handler };

src/api/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"dependencies": {
1818
"@aws-sdk/client-cloudfront-keyvaluestore": "^3.797.0",
1919
"@aws-sdk/client-dynamodb": "^3.797.0",
20+
"@aws-sdk/client-lambda": "^3.835.0",
2021
"@aws-sdk/client-secrets-manager": "^3.797.0",
2122
"@aws-sdk/client-ses": "^3.797.0",
2223
"@aws-sdk/client-sqs": "^3.797.0",
@@ -49,6 +50,7 @@
4950
"ioredis": "^5.6.1",
5051
"jsonwebtoken": "^9.0.2",
5152
"jwks-rsa": "^3.2.0",
53+
"lambda-warmer": "^2.3.0",
5254
"moment": "^2.30.1",
5355
"moment-timezone": "^0.5.48",
5456
"node-cache": "^5.1.2",

0 commit comments

Comments
 (0)