Workflow
The Workflow component lets you add Cloudflare Workflows
to your app.
A Workflow is a durable, multi-step function that runs on Cloudflare Workers. You
define it as a class that extends WorkflowEntrypoint and pass it to this component.
Minimal example
Define a workflow class:
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from "cloudflare:workers";
export class OrderProcessor extends WorkflowEntrypoint<Env, { orderId: string }> { async run(event: WorkflowEvent<{ orderId: string }>, step: WorkflowStep) { await step.do("validate", async () => {}); await step.sleep("cooldown", "10 seconds"); await step.do("charge", async () => {}); }}Then register it with SST:
const processor = new sst.cloudflare.Workflow("OrderProcessor", { handler: "src/workflow.ts", className: "OrderProcessor",});Trigger from a Worker
Link the workflow to a Worker to get a native Cloudflare
Workflow
binding:
new sst.cloudflare.Worker("Api", { handler: "src/api.ts", url: true, link: [processor],});Then invoke it from your worker code:
import { Resource } from "sst";
export default { async fetch(req: Request) { const instance = await Resource.OrderProcessor.create({ params: { orderId: "ord_123" }, }); return Response.json({ id: instance.id }); },};Give the workflow access to other resources
const bucket = new sst.cloudflare.Bucket("Orders");
const processor = new sst.cloudflare.Workflow("OrderProcessor", { handler: "src/workflow.ts", className: "OrderProcessor", link: [bucket],});Constructor
new Workflow(name, args, opts?)Parameters
-
namestring -
argsWorkflowArgs -
opts?ComponentResourceOptions
WorkflowArgs
className
Type Input<string>
The name of the class in your handler file that extends WorkflowEntrypoint.
Cloudflare Workflows are defined as classes that extend the
WorkflowEntrypoint
class. You must specify the name of the class so SST can bind to it.
Given this handler file:
import { WorkflowEntrypoint } from "cloudflare:workers";
export class OrderProcessor extends WorkflowEntrypoint<Env, Params> { async run(event, step) { // ... }}You would set:
{ className: "OrderProcessor"}environment?
Type Input<Record<string, Input<string>>>
Key-value pairs that are set as environment variables on the workflow’s Worker.
They can be accessed in your workflow through this.env.<key>.
{ environment: { DEBUG: "true" }}handler
Type Input<string>
Path to the handler file that exports the class that extends WorkflowEntrypoint.
The handler path is relative to the root of your repo or the sst.config.ts.
{ handler: "src/workflow.ts"}link?
Type Input<any[]>
Link resources to your workflow. This will:
- Make them available on
this.envinside your workflow class. - Allow you to access them in your workflow using the SDK.
Takes a list of components to link to the workflow.
{ link: [bucket, db]}transform?
transform.worker?
Type WorkerArgs | (args: WorkerArgs, opts: ComponentResourceOptions, name: string) => void
Transform the underlying Worker that hosts the workflow class.
transform.workflow?
Type WorkflowArgs | (args: WorkflowArgs, opts: ComponentResourceOptions, name: string) => void
Transform the Workflow resource.
Properties
className
Type Output<string>
The name of the workflow class.
nodes
nodes.worker
Type Worker
The Cloudflare Worker that hosts the workflow class.
nodes.workflow
Type Workflow
The Cloudflare Workflow resource.
scriptName
Type Output<string>
The name of the Worker script that hosts the workflow class.
workflowName
Type Output<string>
The name of the Cloudflare Workflow.
SDK
Use the SDK in your runtime to interact with your infrastructure.
Links
This is accessible through the Resource object in the SDK.
-
classNamestringThe name of the workflow class.
-
scriptNamestringThe name of the Worker script that hosts the workflow class.
-
workflowNamestringThe name of the Cloudflare Workflow.
Bindings
When you link a workflow to a worker, it automatically creates a Workflow binding on the worker. This lets you trigger, inspect, and manage workflow instances from your worker code.
import { Resource } from "sst";
const instance = await Resource.MyWorkflow.create({ params: { hello: "world" } });