Skip to main content

Cron

The Cron construct is a higher level CDK construct that makes it easy to create a cron job. You can create a cron job by handler function and specifying the schedule it needs to run on. Internally this construct uses a EventBridge Rule.

Examples

Rate schedule

import { Cron } from "@serverless-stack/resources";

new Cron(stack, "Cron", {
schedule: "rate(1 minute)",
job: "src/lambda.main",
});

Cron schedule

new Cron(stack, "Cron", {
schedule: "cron(15 10 * * ? *)",
job: "src/lambda.main",
});

Permissions

Allow the function to access S3.

const cron = new Cron(stack, "Cron", {
schedule: "rate(1 minute)",
job: "src/lambda.main",
});

cron.attachPermissions(["s3"]);

Disabling

Disable the cron job from automatically running while developing.

new Cron(stack, "Cron", {
schedule: "rate(1 minute)",
job: "src/lambda.main",
enabled: app.local,
});

Advanced examples

Configuring the event rule

Configure the internally created EventBus Rule.

import { Schedule } from "aws-cdk-lib/aws-events";

new Cron(stack, "Cron", {
job: "src/lambda.main",
cdk: {
rule: {
schedule: Schedule.cron({ minute: "0", hour: "4" }),
}
}
});

Configuring the event target

Configure the internally created EventBus Target.

import { RuleTargetInput } from "aws-cdk-lib/aws-events";

new Cron(stack, "Cron", {
schedule: "rate(1 minute)",
job: {
function: "src/lambda.main",
cdk: {
target: {
event: RuleTargetInput.fromObject({
key: "value"
}),
},
},
},
});

Constructor

new Cron(scope, id, props)

Parameters

CronProps

enabled?

Type : boolean

Default : true

Indicates whether the cron job is enabled.

new Cron(stack, "Cron", {
job: "src/lambda.main",
schedule: "rate(5 minutes)",
enabled: app.local,
})

job

Type : string | Function | CronJobProps

The definition of the function to be executed.

new Cron(stack, "Cron", {
job : "src/lambda.main",
schedule: "rate(5 minutes)",
})

schedule?

Type : rate(${string}) | cron(${string})

The schedule for the cron job. The string format takes a rate expression.

rate(1 minute)
rate(5 minutes)
rate(1 hour)
rate(5 hours)
rate(1 day)
rate(5 days)

Or as a cron expression.

cron(15 10 * * ? *)    // 10:15 AM (UTC) every day.
new Cron(stack, "Cron", {
job: "src/lambda.main",
schedule: "rate(5 minutes)",
});
new Cron(stack, "Cron", {
job: "src/lambda.main",
schedule: "cron(15 10 * * ? *)",
});

cdk.rule?

Type : RuleProps

Override the default settings this construct uses internally to create the events rule.

Properties

An instance of Cron has the following properties.

jobFunction

Type : Function

The internally created Function instance that'll be run on schedule.

cdk.rule

Type : Rule

The internally created CDK EventBridge Rule instance.

Methods

An instance of Cron has the following methods.

attachPermissions

attachPermissions(permissions)

Parameters

Attaches the given list of permissions to the jobFunction. This allows the function to access other AWS resources. Internally calls Function.attachPermissions.

CronJobProps

function

Type : string | Function | FunctionProps

The function that will be executed when the job runs.

  new Cron(stack, "Cron", {
job: {
function: "src/lambda.main",
},
});

cdk.target?

Type : LambdaFunctionProps

Override the default settings this construct uses internally to create the events rule.