Skip to content
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ serverless deploy
| [AWS Simple HTTP Endpoint example in NodeJS with Typescript](https://github.com/serverless/examples/tree/v4/aws-node-http-api-typescript) <br/> This template demonstrates how to make a simple HTTP API with Node.js and Typescript running on AWS Lambda and API Gateway using the Serverless Framework v1. | nodeJS |
| [TypeScript Serverless HTTP API with DynamoDB](https://github.com/serverless/examples/tree/v4/aws-node-http-api-typescript-dynamodb) <br/> This example shows your how to create a TypeScript powered HTTP API with DynamoDB. | nodeJS |
| [AWS S3 File Replicator](https://github.com/serverless/examples/tree/v4/aws-node-s3-file-replicator) <br/> This example creates 2 AWS S3 buckets and copies files in one bucket to the other | nodeJS |
| [AWS Node Scheduled Cron example in NodeJS](https://github.com/serverless/examples/tree/v4/aws-node-scheduled-cron) <br/> A Node.js function on AWS Lambda that runs once a minute, triggered by the Serverless Framework schedule event. | nodeJS |
| [AWS Node Scheduled Cron example in NodeJS](https://github.com/serverless/examples/tree/v4/aws-node-scheduled-cron) <br/> A Node.js function on AWS Lambda that runs once a minute, and one that runs at 9 AM New York time on weekdays, triggered by the Serverless Framework schedule event. | nodeJS |
| [AWS Lambda Self-Managed Code Storage](https://github.com/serverless/examples/tree/v4/aws-node-self-managed-code-storage) <br/> Run Lambda function and layer code directly from the deployment bucket with self-managed code storage | nodeJS |
| [AWS Serving Dynamic HTML via API Gateway example in NodeJS](https://github.com/serverless/examples/tree/v4/aws-node-serve-dynamic-html-via-http-endpoint) <br/> This example illustrates how to hookup an API Gateway endpoint to a Lambda function to render HTML on a GET request. | nodeJS |
| [The Serverless Gong](https://github.com/serverless/examples/tree/v4/aws-node-serverless-gong) <br/> A serverless gong with GitHub and Slack webhooks | nodeJS |
Expand Down Expand Up @@ -150,7 +150,7 @@ serverless deploy
| [AWS Simple HTTP Endpoint example in Python](https://github.com/serverless/examples/tree/v4/aws-python-http-api) <br/> This template demonstrates how to make a simple HTTP API with Python running on AWS Lambda and API Gateway using the Serverless Framework. | python |
| [AWS Serverless HTTP API with DynamoDB store example in Python](https://github.com/serverless/examples/tree/v4/aws-python-http-api-with-dynamodb) <br/> This example demonstrates how to setup an HTTP API allowing you to create, list, get, update and delete Todos. DynamoDB is used to store the data. | python |
| [AWS Serverless HTTP API with DynamoDB store example in Python](https://github.com/serverless/examples/tree/v4/aws-python-http-api-with-pynamodb) <br/> This example demonstrates how to setup an HTTP API allowing you to create, list, get, update and delete Todos. DynamoDB is used to store the data. | python |
| [AWS Python Scheduled Cron example in Python](https://github.com/serverless/examples/tree/v4/aws-python-scheduled-cron) <br/> A Python function on AWS Lambda that runs once a minute, triggered by the Serverless Framework schedule event. | python |
| [AWS Python Scheduled Cron example in Python](https://github.com/serverless/examples/tree/v4/aws-python-scheduled-cron) <br/> A Python function on AWS Lambda that runs once a minute, and one that runs at 9 AM New York time on weekdays, triggered by the Serverless Framework schedule event. | python |
| [AWS Simple HTTP Endpoint example in Python](https://github.com/serverless/examples/tree/v4/aws-python-simple-http-endpoint) <br/> This example demonstrates how to setup a simple HTTP GET endpoint. Once you ping it, it will reply with the current time. | python |
| [Python SQS Producer Consumer on AWS](https://github.com/serverless/examples/tree/v4/aws-python-sqs-worker) <br/> This template demonstrates how to develop and deploy a simple SQS-based producer-consumer service running on AWS Lambda using the traditional Serverless Framework. | python |
| [Python Telegram Bot](https://github.com/serverless/examples/tree/v4/aws-python-telegram-bot) <br/> This example demonstrates how to setup an echo Telegram Bot using the Serverless Framework. | python |
Expand Down
72 changes: 59 additions & 13 deletions aws-node-scheduled-cron/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--
title: 'AWS Node Scheduled Cron example in NodeJS'
description: 'A Node.js function on AWS Lambda that runs once a minute, triggered by the Serverless Framework schedule event.'
description: 'A Node.js function on AWS Lambda that runs once a minute, and one that runs at 9 AM New York time on weekdays, triggered by the Serverless Framework schedule event.'
layout: Doc
framework: v4
platform: AWS
Expand All @@ -15,13 +15,13 @@ authorAvatar: 'https://avatars3.githubusercontent.com/u/5679763?v=4&s=140'

[![Verified](https://raw.githubusercontent.com/serverless/examples/verified/badges/aws-node-scheduled-cron.svg)](https://github.com/serverless/examples/blob/verified/results.json)

This template deploys a Node.js function on AWS Lambda that runs once a minute, like a cron job. Use it as the starting point for periodic work such as cleanups, reports or polling.
This template deploys two Node.js functions on AWS Lambda that run on a schedule, like cron jobs: one runs once a minute, the other at 9 AM New York time on weekdays. Use it as the starting point for periodic work such as cleanups, reports or polling.

## What it deploys

- A Lambda function, `rateHandler`.
- An EventBridge rule that invokes the function every minute.
- An IAM role for the function and a CloudWatch Logs log group for its logs.
- A Lambda function, `rateHandler`, and an EventBridge rule that invokes it every minute.
- A Lambda function, `cronHandler`, and an EventBridge Scheduler schedule that invokes it at 9 AM on weekdays in the `America/New_York` time zone.
- An IAM role for the functions, which EventBridge Scheduler also uses to invoke `cronHandler`, and a CloudWatch Logs log group for each function.

## Prerequisites

Expand All @@ -46,10 +46,11 @@ You should see output like this:
Deploying "aws-node-scheduled-cron" to stage "dev" (us-east-1)
✔ Service deployed to stack aws-node-scheduled-cron-dev (103s)
functions:
rateHandler: aws-node-scheduled-cron-dev-rateHandler (2.7 kB)
rateHandler: aws-node-scheduled-cron-dev-rateHandler (4.3 kB)
cronHandler: aws-node-scheduled-cron-dev-cronHandler (4.3 kB)
```

The schedule is active as soon as the deploy finishes; there is no further step.
The schedules are active as soon as the deploy finishes; there is no further step.

## Try it

Expand All @@ -74,33 +75,78 @@ REPORT - fe4200af-617b-483d-ad33-13c010a52dcf
Duration: 1.95 ms Billed Duration: 2 ms Memory Size: 1024 MB Max Memory Used: 78 MB
```

To see the scheduled runs, read the function's logs. Each minute adds one line:
To see the scheduled runs, read the function's logs. The first scheduled run comes within about a minute of the deploy, and each minute adds one more, next to the runs you invoked yourself; if no scheduled run is there yet, wait a minute and run the command again:

```bash
serverless logs --function rateHandler
```

`cronHandler` runs only at 9 AM on weekdays. To run it now, invoke it the same way:

```bash
serverless invoke --function cronHandler --log
```

The log contains the line `cronHandler` writes, `Your weekday cron function ran at`, followed by the current date and time:

```text
null

----------------------

START - a15ae131-f467-45d4-942f-b66be46e65b0 - Version: $LATEST
2026-11-25 21:57:46.962 - a15ae131-f467-45d4-942f-b66be46e65b0 -
INFO Your weekday cron function ran at Wed Nov 25 2026 21:57:46 GMT+0000 (Coordinated Universal Time)

REPORT - a15ae131-f467-45d4-942f-b66be46e65b0
Duration: 6.77 ms Billed Duration: 143 ms Memory Size: 1024 MB Max Memory Used: 77 MB Init Duration: 135.98 ms
```

`serverless invoke` calls the function directly, not through its schedule, so you can test a cron function at any time of day.

## How it works

`serverless.yml` attaches a `schedule` event to the function. The Framework turns it into an EventBridge rule:
`serverless.yml` attaches a `schedule` event to each function:

```yaml
functions:
rateHandler:
handler: handler.run
events:
- schedule: rate(1 minute)
cronHandler:
handler: handler.cron
events:
- schedule:
method: scheduler
rate: cron(0 9 ? * MON-FRI *)
timezone: America/New_York
```

`rate(1 minute)` runs the function every minute. A `cron(...)` expression runs it at fixed times instead; the [schedule event docs](https://www.serverless.com/framework/docs/providers/aws/events/schedule) list both forms and their options.
A schedule is written as a rate or a cron expression:

- `rate(1 minute)` runs the function at a fixed interval. The unit is `minute`, `hour` or `day` for a value of 1, and `minutes`, `hours` or `days` for larger values.
- `cron(0 9 ? * MON-FRI *)` runs it at fixed times. A cron expression has six fields: minutes, hours, day of month, month, day of week and year. One of day of month and day of week must be `?`, because you can't set both: here the day of week is set (`MON-FRI`), so the day of month is `?`.

The two functions also use the two ways the Framework creates a schedule:

`handler.js` logs the time of each run:
- **EventBridge rule**, the default. `rateHandler`'s event becomes a scheduled rule on the default event bus. A rule evaluates cron expressions in UTC.
- **EventBridge Scheduler**, with `method: scheduler`. `cronHandler`'s event becomes an EventBridge Scheduler schedule, which evaluates the cron expression in the time zone set in `timezone`, an IANA name such as `America/New_York`, and follows daylight saving time. Without `timezone`, Scheduler uses UTC. Scheduler invokes the function with the function's IAM role, and the Framework adds what that needs to the role.

To run a cron schedule at a local time, use `method: scheduler` with `timezone`. The [schedule event docs](https://www.serverless.com/framework/docs/providers/aws/events/schedule) list all options of both methods, and the [AWS docs on schedule expressions](https://docs.aws.amazon.com/scheduler/latest/UserGuide/schedule-types.html) describe the cron syntax.

`handler.js` logs the time of each run, with a different line for each function:

```javascript
exports.run = async () => {
const time = new Date();
console.log(`Your cron function ran at ${time}`);
};

exports.cron = async () => {
const time = new Date();
console.log(`Your weekday cron function ran at ${time}`);
};
```

### Develop with Dev Mode
Expand All @@ -117,14 +163,14 @@ Press Ctrl+C to end the session; the function runs the deployed code again. Run

## Cost and cleanup

The rule invokes the function every minute for as long as it is deployed, so Lambda bills an invocation and its compute time every minute, whether or not you look at the results. Each run also adds a line to the function's logs in CloudWatch Logs, which bills for the log data it stores. See [Lambda pricing](https://aws.amazon.com/lambda/pricing/), [EventBridge pricing](https://aws.amazon.com/eventbridge/pricing/) and [CloudWatch pricing](https://aws.amazon.com/cloudwatch/pricing/). Remove the stack, with the function, the rule and the log group, when you are done:
The rule invokes `rateHandler` every minute and the schedule invokes `cronHandler` five times a week, for as long as they are deployed, so Lambda bills each invocation and its compute time, whether or not you look at the results. EventBridge Scheduler bills each invocation it makes. Each run also adds lines to the function's logs in CloudWatch Logs, which bills for the log data it stores. See [Lambda pricing](https://aws.amazon.com/lambda/pricing/), [EventBridge pricing](https://aws.amazon.com/eventbridge/pricing/) and [CloudWatch pricing](https://aws.amazon.com/cloudwatch/pricing/). Remove the stack, with the functions, the rule, the schedule and the log groups, when you are done:

```bash
serverless remove
```

## Next steps

- Read the [schedule event docs](https://www.serverless.com/framework/docs/providers/aws/events/schedule) for cron expressions, inputs and disabling a schedule.
- Read the [schedule event docs](https://www.serverless.com/framework/docs/providers/aws/events/schedule) for inputs, names and disabling a schedule.
- Start from a function with no events in the [AWS Node.js example](https://github.com/serverless/examples/tree/v4/aws-node).
- Process messages from a queue instead of a timer with the [SQS worker example](https://github.com/serverless/examples/tree/v4/aws-node-sqs-worker).
12 changes: 11 additions & 1 deletion aws-node-scheduled-cron/example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "AWS Node Scheduled Cron example in NodeJS",
"description": "A Node.js function on AWS Lambda that runs once a minute, triggered by the Serverless Framework schedule event.",
"description": "A Node.js function on AWS Lambda that runs once a minute, and one that runs at 9 AM New York time on weekdays, triggered by the Serverless Framework schedule event.",
"language": "node",
"platform": "AWS",
"framework": "v4",
Expand All @@ -13,6 +13,7 @@
"services": [
"lambda",
"eventbridge",
"scheduler",
"iam",
"logs"
],
Expand All @@ -28,6 +29,15 @@
],
"contains": "Your cron function ran at"
}
},
{
"invoke": {
"function": "cronHandler",
"args": [
"--log"
],
"contains": "Your weekday cron function ran at"
}
}
]
}
Expand Down
5 changes: 5 additions & 0 deletions aws-node-scheduled-cron/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ exports.run = async () => {
const time = new Date();
console.log(`Your cron function ran at ${time}`);
};

exports.cron = async () => {
const time = new Date();
console.log(`Your weekday cron function ran at ${time}`);
};
7 changes: 7 additions & 0 deletions aws-node-scheduled-cron/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ functions:
handler: handler.run
events:
- schedule: rate(1 minute)
cronHandler:
handler: handler.cron
events:
- schedule:
method: scheduler
rate: cron(0 9 ? * MON-FRI *)
timezone: America/New_York
Loading