Skip to content
GitHub Discord

AWS Task Cron

Use the Task and Cron components for long running background tasks.

We have a node script that we want to run in index.mjs. It’ll be deployed as a Docker container using Dockerfile.

It’ll be invoked by a cron job that runs every 2 minutes.

sst.config.ts
new sst.aws.Cron("MyCron", {
task,
schedule: "rate(2 minutes)"
});

When this is run in sst dev, the task is executed locally using dev.command.

sst.config.ts
dev: {
command: "node index.mjs"
}

To deploy, you need the Docker daemon running.

sst.config.ts
const bucket = new sst.aws.Bucket("MyBucket");
const vpc = new sst.aws.Vpc("MyVpc");
const cluster = new sst.aws.Cluster("MyCluster", { vpc });
const task = new sst.aws.Task("MyTask", {
cluster,
link: [bucket],
dev: {
command: "node index.mjs",
},
});
new sst.aws.Cron("MyCron", {
task,
schedule: "rate(2 minutes)",
});

View the full example.