Skip to content
This repository was archived by the owner on Jul 11, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ functions:
- http:
path: hello
method: get
# Ping every 5 minutes to avoid cold starts
- schedule:
rate: rate(5 minutes)
enabled: true
```

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).
Expand Down Expand Up @@ -108,27 +104,27 @@ Lambda functions will go "cold" if they haven't been invoked for a certain perio
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`:

```yaml
functions:
myFunc:
handler: src/myFunc.default
timeout: 10
memorySize: 256
custom:
warmup:
enabled: true
events:
# ...other config happening up here and then...
# Ping every 5 minutes to avoid cold starts
- schedule:
rate: rate(5 minutes)
enabled: true
- schedule: rate(5 minutes)
prewarm: true
concurrency: 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add some explanatory text here. E.g., does this just warm all your lambdas now?

If so, maybe text like:

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.

I don't know if the above is correct, so can you verify?

Also it's worth pointing out that custom is for setting project-wide warmup behaviors, but you can set per-function behavior by setting config under a warmup key.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all correct!

```

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.

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).

Your handler function can then handle this event like so:

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

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
"test": "jest",
"test:watch": "jest --watch",
"build": "serverless webpack",
"watch:hello": "serverless invoke local --watch --function hello --path fixtures/event.json",
"watch:warm": "serverless invoke local --watch --function hello --path fixtures/scheduled.json"
"watch:hello": "serverless invoke local --watch --function hello --path fixtures/event.json"
},
"devDependencies": {
"@babel/core": "7.3.3",
Expand All @@ -41,9 +40,10 @@
"lint-staged": "^8.0.0",
"nodemon": "^1.18.9",
"prettier": "^1.14.2",
"serverless": "^1.32.0",
"serverless": "^1.40.0",
"serverless-dotenv-plugin": "^2.0.1",
"serverless-offline": "^4.0.0",
"serverless-plugin-warmup": "^4.5.3-rc.1",
"serverless-webpack": "^5.2.0",
"ts-jest": "^24.0.0",
"ts-loader": "^5.3.1",
Expand Down
20 changes: 12 additions & 8 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,23 @@ provider:
# you can overwrite defaults here
# stage: dev
# region: us-east-1
iamRoleStatements:
- Effect: 'Allow'
Action:
- 'lambda:InvokeFunction'
Resource: '*'

custom:
webpackIncludeModules: true
webpack:
webpackConfig: ./webpack.config.js
packager: 'yarn' # Packager that will be used to package your external modules
warmup:
enabled: true
events:
- schedule: rate(5 minutes)
prewarm: true
concurrency: 1

# you can add statements to the Lambda function's IAM Role here
# iamRoleStatements:
Expand Down Expand Up @@ -73,20 +84,12 @@ functions:
- http:
path: hello-typescript
method: get
# Ping every 5 minutes to avoid cold starts
- schedule:
rate: rate(5 minutes)
enabled: true
hello:
handler: src/hello.default
events:
- http:
path: hello
method: get
# Ping every 5 minutes to avoid cold starts
- schedule:
rate: rate(5 minutes)
enabled: true

# The following are a few example events you can configure
# NOTE: Please make sure to change your handler code to work with those events
Expand Down Expand Up @@ -119,4 +122,5 @@ functions:
plugins:
- serverless-webpack
- serverless-offline
- serverless-plugin-warmup
- serverless-dotenv-plugin
2 changes: 1 addition & 1 deletion src/utils/run-warm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const runWarm = (lambdaFunc: AWSLambda.Handler): AWSLambda.Handler => (
) => {
// Detect the keep-alive ping from CloudWatch and exit early. This keeps our
// lambda function running hot.
if (event.source === 'aws.events') {
if (event.source === 'serverless-plugin-warmup') {
return callback(null, 'pinged');
}

Expand Down
Loading