Skip to content
GitHub Discord

AWS Task

Use the Task component to run background tasks.

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

We also have a function that the task is linked to. It uses the SDK to start the task.

index.ts
import { Resource } from "sst";
import { task } from "sst/aws/task";
export const handler = async () => {
const ret = await task.run(Resource.MyTask);
return {
statusCode: 200,
body: JSON.stringify(ret, null, 2),
};
};

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", { nat: "ec2" });
const cluster = new sst.aws.Cluster("MyCluster", { vpc });
const task = new sst.aws.Task("MyTask", {
cluster,
public: true,
link: [bucket],
image: {
context: "image",
},
dev: {
command: "node index.mjs",
},
});
new sst.aws.Function("MyApp", {
vpc,
url: true,
link: [task],
handler: "index.handler",
});

View the full example.