Skip to content
This repository was archived by the owner on Jul 11, 2024. It is now read-only.

Commit bd8d4f6

Browse files
authored
feat: add warmup plugin (#57)
* feat: add warmup plugin * fix: remove old configs * docs: update readme * docs: update readme * docs: update readme
1 parent 4fa2ac7 commit bd8d4f6

File tree

5 files changed

+8652
-8252
lines changed

5 files changed

+8652
-8252
lines changed

README.md

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ functions:
4242
- http:
4343
path: hello
4444
method: get
45-
# Ping every 5 minutes to avoid cold starts
46-
- schedule:
47-
rate: rate(5 minutes)
48-
enabled: true
4945
```
5046
5147
Ignoring the scheduling event, you can see here that we're setting up a function named `hello` with a handler at `src/hello.js` (the `.default` piece is just indicating that the function to run will be the default export from that file). The `http` event says that this function will run when an http event is triggered (on AWS, this happens via API Gateway).
@@ -108,27 +104,27 @@ Lambda functions will go "cold" if they haven't been invoked for a certain perio
108104
A frequently running function won't have this problem, but you can keep your function running hot by scheduling a regular ping to your lambda function. Here's what that looks like in your `serverless.yml`:
109105

110106
```yaml
111-
functions:
112-
myFunc:
113-
handler: src/myFunc.default
114-
timeout: 10
115-
memorySize: 256
107+
custom:
108+
warmup:
109+
enabled: true
116110
events:
117-
# ...other config happening up here and then...
118-
# Ping every 5 minutes to avoid cold starts
119-
- schedule:
120-
rate: rate(5 minutes)
121-
enabled: true
111+
- schedule: rate(5 minutes)
112+
prewarm: true
113+
concurrency: 2
122114
```
123115

116+
The above config would keep all of your deployed lambda functions running warm. The `prewarm` flag will ensure your function is warmed immediately after deploys (so you don't have to wait five minutes for the first scheduled event). And by setting the `concurrency` to `2`, we're keeping two instances warm for each deployed function.
117+
118+
Under `custom.warmup`, you can set project-wide warmup behaviors. On the other hand, if you want to set function-specific behaviours, you should use the `warmup` key under the select functions. You can browse all the options [here](https://www.npmjs.com/package/serverless-plugin-warmup#configuration).
119+
124120
Your handler function can then handle this event like so:
125121

126122
```javascript
127123
const myFunc = (event, context, callback) => {
128124
// Detect the keep-alive ping from CloudWatch and exit early. This keeps our
129125
// lambda function running hot.
130-
if (event.source === 'aws.events') {
131-
// aws.events is the source for Scheduled events
126+
if (event.source === 'serverless-plugin-warmup') {
127+
// serverless-plugin-warmup is the source for Scheduled events
132128
return callback(null, 'pinged');
133129
}
134130

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
"test": "jest",
1717
"test:watch": "jest --watch",
1818
"build": "serverless webpack",
19-
"watch:hello": "serverless invoke local --watch --function hello --path fixtures/event.json",
20-
"watch:warm": "serverless invoke local --watch --function hello --path fixtures/scheduled.json"
19+
"watch:hello": "serverless invoke local --watch --function hello --path fixtures/event.json"
2120
},
2221
"devDependencies": {
2322
"@babel/core": "7.3.3",
@@ -41,9 +40,10 @@
4140
"lint-staged": "^8.0.0",
4241
"nodemon": "^1.18.9",
4342
"prettier": "^1.14.2",
44-
"serverless": "^1.32.0",
43+
"serverless": "^1.40.0",
4544
"serverless-dotenv-plugin": "^2.0.1",
4645
"serverless-offline": "^4.0.0",
46+
"serverless-plugin-warmup": "^4.5.3-rc.1",
4747
"serverless-webpack": "^5.2.0",
4848
"ts-jest": "^24.0.0",
4949
"ts-loader": "^5.3.1",

serverless.yml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,23 @@ provider:
3131
# you can overwrite defaults here
3232
# stage: dev
3333
# region: us-east-1
34+
iamRoleStatements:
35+
- Effect: 'Allow'
36+
Action:
37+
- 'lambda:InvokeFunction'
38+
Resource: '*'
3439

3540
custom:
3641
webpackIncludeModules: true
3742
webpack:
3843
webpackConfig: ./webpack.config.js
3944
packager: 'yarn' # Packager that will be used to package your external modules
45+
warmup:
46+
enabled: true
47+
events:
48+
- schedule: rate(5 minutes)
49+
prewarm: true
50+
concurrency: 1
4051

4152
# you can add statements to the Lambda function's IAM Role here
4253
# iamRoleStatements:
@@ -73,20 +84,12 @@ functions:
7384
- http:
7485
path: hello-typescript
7586
method: get
76-
# Ping every 5 minutes to avoid cold starts
77-
- schedule:
78-
rate: rate(5 minutes)
79-
enabled: true
8087
hello:
8188
handler: src/hello.default
8289
events:
8390
- http:
8491
path: hello
8592
method: get
86-
# Ping every 5 minutes to avoid cold starts
87-
- schedule:
88-
rate: rate(5 minutes)
89-
enabled: true
9093

9194
# The following are a few example events you can configure
9295
# NOTE: Please make sure to change your handler code to work with those events
@@ -119,4 +122,5 @@ functions:
119122
plugins:
120123
- serverless-webpack
121124
- serverless-offline
125+
- serverless-plugin-warmup
122126
- serverless-dotenv-plugin

src/utils/run-warm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const runWarm = (lambdaFunc: AWSLambda.Handler): AWSLambda.Handler => (
55
) => {
66
// Detect the keep-alive ping from CloudWatch and exit early. This keeps our
77
// lambda function running hot.
8-
if (event.source === 'aws.events') {
8+
if (event.source === 'serverless-plugin-warmup') {
99
return callback(null, 'pinged');
1010
}
1111

0 commit comments

Comments
 (0)