| description | Docs for the sst.Cron construct in the @serverless-stack/resources package. This construct creates a CDK event rule. |
|---|
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.
new Cron(scope: Construct, id: string, props: CronProps)Parameters
import { Cron } from "@serverless-stack/resources";
new Cron(this, "Cron", {
schedule: "rate(1 minute)",
job: "src/lambda.main",
});new Cron(this, "Cron", {
schedule: "cron(15 10 * * ? *)",
job: "src/lambda.main",
});import { Duration } from "@aws-cdk/core";
new Cron(this, "Cron", {
schedule: Duration.days(1),
job: "src/lambda.main",
});new Cron(this, "Cron", {
schedule: { minute: "0", hour: "4" },
job: "src/lambda.main",
});Allow the function to access S3.
const cron = new Cron(this, "Cron", {
schedule: "rate(1 minute)",
job: "src/lambda.main",
});
cron.attachPermissions(["s3"]);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"
}),
},
},
});An instance of Cron contains the following properties.
Type : cdk.aws-events.Rule
The internally created CDK EventBridge Rule instance.
Type : Function
The internally created Function instance that'll be run on schedule.
An instance of Queue contains the following methods.
attachPermissions(permissions: Permissions)Parameters
- permissions
Permissions
Attaches the given list of permissions to the jobFunction. This allows the function to access other AWS resources.
Internally calls Function.attachPermissions.
Type : FunctionDefinition | CronJobProps, defaults to undefined
Takes FunctionDefinition or CronJobProps object used to create the function for the cron job.
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 * * ? *)"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.
Type : FunctionDefinition
A FunctionDefinition object that'll be used to create the job function for the cron.
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.