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.

Initializer#

new Cron(scope: Construct, id: string, props: CronProps)

Parameters

Examples#

Using the rate expression#

import { Cron } from "@serverless-stack/resources";
new Cron(this, "Cron", {
schedule: "rate(1 minute)",
job: "src/lambda.main",
});

Using the cron expression#

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

Using Duration#

import { Duration } from "@aws-cdk/core";
new Cron(this, "Cron", {
schedule: Duration.days(1),
job: "src/lambda.main",
});

Using CronOptions#

new Cron(this, "Cron", {
schedule: { minute: "0", hour: "4" },
job: "src/lambda.main",
});

Giving the cron job some permissions#

Allow the function to access S3.

const cron = new Cron(this, "Cron", {
schedule: "rate(1 minute)",
job: "src/lambda.main",
});
cron.attachPermissions(["s3"]);

Configuring the job#

Configure the internally created CDK Event Target.

import { RuleTargetInput } from "@aws-cdk/aws-events";
new Cron(this, "Cron", {
schedule: "rate(1 minute)",
job: {
function: "src/lambda.main",
jobProps: {
event: RuleTargetInput.fromObject({
key: "value"
}),
},
},
});

Properties#

An instance of Cron contains the following properties.

eventsRule#

Type : cdk.aws-events.Rule

The internally created CDK EventBridge Rule instance.

jobFunction#

Type : Function

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

Methods#

An instance of Queue contains the following methods.

attachPermissions#

attachPermissions(permissions: Permissions)

Parameters

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

Internally calls Function.attachPermissions.

CronProps#

job#

Type : FunctionDefinition | CronJobProps, defaults to undefined

Takes FunctionDefinition or CronJobProps object used to create the function for the cron job.

schedule?#

Type : string | cdk.Duration | cdk.aws-events.CronOptions

The schedule for the cron job. Can be specified as a string. The string format takes a rate expression.

"rate(_Value Unit_)"
// For example, every 5 minutes
"rate(5 minutes)"

Or as a cron expression.

"cron(Minutes Hours Day-of-month Month Day-of-week Year)"
// For example, 10:15 AM (UTC) every day
"cron(15 10 * * ? *)"

You can also use the cdk.Duration as an alternative to defining the rate expression.

import { Duration } from "@aws-cdk/core";
// Repeat every 5 minutes
// As cdk.Duration
Duration.minutes(5)
// The equivalent rate expression
"rate(5 minutes)"

Similarly, you can specify the cron expression using cdk.aws-events.CronOptions.

// 10:15 AM (UTC) every day
// As cdk.aws-events.CronOptions
{ minute: "15", hour: "10" }
// The equivalent cron expression
"cron(15 10 * * ? *)"

eventsRule?#

Type : cdk.aws-events.RuleProps, defaults to undefined

Or optionally pass in a CDK EventBridge RuleProps. This allows you to override the default settings this construct uses internally to create the events rule.

CronJobProps#

function#

Type : FunctionDefinition

A FunctionDefinition object that'll be used to create the job function for the cron.

jobProps?#

Type : cdk.aws-events-targets.LambdaFunctionProps, defaults to undefined

Or optionally pass in a CDK LambdaFunctionProps. This allows you to override the default settings this construct uses internally to create the job.