This plugin enables you to schedule Lambda functions with a variety of cron job frequencies. It supports different stages (dev
, test
, production
) with options for various timing intervals such as minutes, daily, weekly, and monthly schedules.
Note: This plugin is built specifically for the
AWS
provider.
- Configure schedules for Lambda functions across different stages.
- Flexible scheduling options:
interval
: Schedule to run at intervals (minutes, hours, days).daily
: Specific time of the day.weekly
: Specific day of the week at a specific time.monthly
: Specific day of the month at a specific time.
- Pass custom input parameters to Lambda functions.
-
First, add it to your project as a dev dependency:
-
First, add it to your project as a dev dependency:
- npm:
npm i -D lambda-cron
- pnpm:
pnpm i -D lambda-cron
- yarn:
yarn add -D lambda-cron
- npm:
-
Include it in your plugins:
plugins: - lambda-cron
You can schedule a Lambda function to run at specific intervals. For example, a job with the unit minutes
will run every x
minutes.
unit
: required
- Interval unit can be one of the following:
days
hours
minutes
duration
: required
- Time interval after which the job will run again.
// The hello Lambda function will run every 2 minutes.
{
custom: {
'lambda-cron': {
dev: {
hello: {
schedule: {
type: 'interval',
params: {
unit: 'minutes',
duration: 2,
},
},
},
},
},
},
}
# The hello Lambda function will run every 2 days.
custom:
lambda-cron:
dev:
hello:
schedule:
type: interval
params:
unit: days
duration: 2
With a daily
schedule, you can run a job at a specific time of the day.
hour
:required
- Hour of the day in 24-hour format.minute
:optional
- Minute of the hour (1-59). Defaults to0
if not provided.
{
schedule: {
type: 'daily',
params: {
hour: <hour-of-the-day>, // required
minute: <minute-of-the-hour>, // optional, defaults to 0
},
},
}
schedule:
type: daily
params:
hour: <hour-of-the-day> # required
minute: <minute-of-the-hour> # optional, defaults to 0
With a weekly
schedule, you can run a job on a specific day of the week at a specified time.
day
:required
- The day of the week (e.g.,sunday
,monday
).hour
:optional
- Hour of the day in 24-hour format (defaults to 0).minute
:optional
- Minute of the hour (defaults to 0).
schedule:
type: weekly
params:
day: <day-of-the-week> # required
hour: <hour-of-the-day> # optional, defaults to 0
minute: <minute-of-the-hour> # optional, defaults to 0
With a monthly
schedule, you can run a job on a specific day of the month at a specified time.
day
:required
- Day of the month (1-31).hour
:optional
- Hour of the day in 24-hour format (defaults to 0).minute
:optional
- Minute of the hour (defaults to 0).
schedule:
type: monthly
params:
day: <day-of-the-month> # required
hour: <hour-of-the-day> # optional, defaults to 0
minute: <minute-of-the-hour> # optional, defaults to 0
With a yearly
schedule, you can run a job on a specific day of the year at a specified time.
month
:required
- Month of the year (1-12).day
:optional
- Day of the month 1-31. (defaults to 1)hour
:optional
- Hour of the day in 24-hour format (defaults to 0).minute
:optional
- Minute of the hour (defaults to 0).
schedule:
type: yearly
params:
month: <month-oof-the-year> # required
day: <day-of-the-month> # optional, defaults to 1
hour: <hour-of-the-day> # optional, defaults to 0
minute: <minute-of-the-hour> # optional, defaults to 0
Below is an example of how to configure the lambda-cron
plugin for the dev
stage:
{
service: 'aws-serverless-typescript-project',
frameworkVersion: '3',
plugins: ['serverless-esbuild', 'lambda-cron'],
provider: {
name: 'aws',
},
functions: {
hello,
books
},
custom: {
'lambda-cron': {
dev: {
books: {
schedule: {
type: 'interval',
params: {
unit: 'minute',
duration: 2,
},
},
input: {
key: 'value',
},
},
},
},
},
}
service: aws-serverless-typescript-project
frameworkVersion: '3'
plugins:
- serverless-esbuild
- lambda-cron
provider:
name: aws
runtime: nodejs20.x
custom:
lambda-cron:
dev:
books:
schedule:
type: monthly
params:
day: 2
hour: 2
minute: 2
input:
key: value