You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 11, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+12-16Lines changed: 12 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,10 +42,6 @@ functions:
42
42
- http:
43
43
path: hello
44
44
method: get
45
-
# Ping every 5 minutes to avoid cold starts
46
-
- schedule:
47
-
rate: rate(5 minutes)
48
-
enabled: true
49
45
```
50
46
51
47
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
108
104
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`:
109
105
110
106
```yaml
111
-
functions:
112
-
myFunc:
113
-
handler: src/myFunc.default
114
-
timeout: 10
115
-
memorySize: 256
107
+
custom:
108
+
warmup:
109
+
enabled: true
116
110
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
122
114
```
123
115
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
+
124
120
Your handler function can then handle this event like so:
125
121
126
122
```javascript
127
123
const myFunc = (event, context, callback) => {
128
124
// Detect the keep-alive ping from CloudWatch and exit early. This keeps our
129
125
// 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
0 commit comments