Cron
Reference doc for the `sst.aws.Cron` component.
The Cron
component lets you add cron jobs to your app
using Amazon Event Bus. The cron job can invoke a Function
or a container Task
.
Cron job function
Pass in a schedule
and a function
that’ll be executed.
new sst.aws.Cron("MyCronJob", { function: "src/cron.handler", schedule: "rate(1 minute)"});
Cron job container task
Create a container task and pass in a schedule
and a task
that’ll be executed.
const myCluster = new sst.aws.Cluster("MyCluster");const myTask = new sst.aws.Task("MyTask", { cluster: myCluster });
new sst.aws.Cron("MyCronJob", { task: myTask, schedule: "rate(1 day)"});
Customize the function
new sst.aws.Cron("MyCronJob", { schedule: "rate(1 minute)", function: { handler: "src/cron.handler", timeout: "60 seconds" }});
Constructor
new Cron(name, args, opts?)
Parameters
-
name
string
-
args
CronArgs
-
opts?
ComponentResourceOptions
CronArgs
enabled?
Type Input
<
boolean
>
Default true
Configures whether the cron job is enabled. When disabled, the cron job won’t run.
{ enabled: false}
event?
Type Input
<
Record
<
string
, Input
<
string
>
>
>
The event that’ll be passed to the function or task.
{ event: { foo: "bar", }}
For Lambda functions, the event will be passed to the function as an event.
function handler(event) { console.log(event.foo);}
For ECS Fargate tasks, the event will be passed to the task as the SST_EVENT
environment variable.
const event = JSON.parse(process.env.SST_EVENT);console.log(event.foo);
function?
Type Input
<
string
|
FunctionArgs
|
“arn:aws:lambda:${string}”
>
The function that’ll be executed when the cron job runs.
{ function: "src/cron.handler"}
You can pass in the full function props.
{ function: { handler: "src/cron.handler", timeout: "60 seconds" }}
You can also pass in a function ARN.
{ function: "arn:aws:lambda:us-east-1:000000000000:function:my-sst-app-jayair-MyFunction",}
schedule
Type Input
<
“
rate(${string})
”
|
“
cron(${string})
”
>
The schedule for the cron job.
You can use a rate expression.
{ schedule: "rate(5 minutes)" // schedule: "rate(1 minute)" // schedule: "rate(5 minutes)" // schedule: "rate(1 hour)" // schedule: "rate(5 hours)" // schedule: "rate(1 day)" // schedule: "rate(5 days)"}
Or a cron expression.
{ schedule: "cron(15 10 * * ? *)", // 10:15 AM (UTC) every day}
task?
Type Task
The task that’ll be executed when the cron job runs.
For example, let’s say you have a task.
const myCluster = new sst.aws.Cluster("MyCluster");const myTask = new sst.aws.Task("MyTask", { cluster: myCluster });
You can then pass in the task to the cron job.
new sst.aws.Cron("MyCronJob", { task: myTask, schedule: "rate(1 minute)"});
transform?
transform.rule?
Type EventRuleArgs
|
(
args
:
EventRuleArgs
,
opts
:
ComponentResourceOptions
,
name
:
string
)
=>
void
Transform the EventBridge Rule resource.
transform.target?
Type EventTargetArgs
|
(
args
:
EventTargetArgs
,
opts
:
ComponentResourceOptions
,
name
:
string
)
=>
void
Transform the EventBridge Target resource.
Properties
nodes
nodes.rule
Type EventRule
The EventBridge Rule resource.
nodes.target
Type EventTarget
The EventBridge Target resource.
nodes.function
Type Output
<
Function
>
The AWS Lambda Function that’ll be invoked when the cron job runs.
nodes.job
Type Output
<
Function
>
The AWS Lambda Function that’ll be invoked when the cron job runs.