Overview
A Serverless Workflow is a sequence of specific tasks that are executed in a defined order. By default, this order follows the declaration sequence within the workflow definition. Workflows are designed to automate processes and orchestrate various serverless functions and services. Workflows can be triggered in different ways:- On request: Manually initiated workflow execution
- Scheduled: Using CRON expressions for time-based triggers
- Event-driven: Initiated upon correlation with specific events
Workflow Structure
A workflow definition consists of several key sections that define its behavior and components:Document Metadata
Every workflow begins with document metadata that identifies the workflow:The version of the Serverless Workflow DSL being used
The namespace for organizing workflows
A unique name for the workflow
The semantic version of the workflow
Use Section
Theuse section defines reusable components that can be referenced throughout the workflow:
Defines authentication policies that can be referenced by name
Defines external resource catalogs for reusable functions
Defines extensions that modify task behavior
Defines reusable error definitions
Defines reusable function definitions
Defines reusable retry policies
Lists secrets required by the workflow
Do Section
Thedo section contains the sequence of tasks to execute:
Tasks in the
do section are executed sequentially by default, unless wrapped in a fork task for parallel execution.Status Phases
Both workflows and tasks can exist in several phases, each indicating the current state of execution:| Phase | Description |
|---|---|
pending | The workflow has been initiated and is pending execution |
running | The workflow is currently in progress |
waiting | The workflow execution is temporarily paused, awaiting either inbound event(s) or a specified time interval as defined by a wait task |
suspended | The workflow execution has been manually paused by a user and will remain halted until explicitly resumed |
cancelled | The workflow execution has been terminated before completion |
faulted | The workflow execution has encountered an error |
completed | The workflow ran to completion |
Status Phase Transitions
The flow of execution within a workflow can be controlled using flow directives, which provide instructions to the workflow engine on how to manage and handle specific aspects of workflow execution.
Lifecycle Events
Lifecycle Cloud Events standardize notifications for key state changes in workflows and tasks. These events carry consistent information like IDs, status transitions, timestamps, and relevant metadata, enabling users to reliably respond to updates, enhancing interoperability and simplifying integration across different implementations. Runtimes are expected to publish these events upon state changes. While using the HTTP protocol binding with structured content mode is recommended, other transports adhering to the CloudEvents specification may be used.Workflow Lifecycle Events
| Type | Required | Description |
|---|---|---|
io.serverlessworkflow.workflow.started.v1 | yes | Notifies about the start of a workflow |
io.serverlessworkflow.workflow.suspended.v1 | yes | Notifies about suspending a workflow execution |
io.serverlessworkflow.workflow.resumed.v1 | yes | Notifies about resuming a workflow execution |
io.serverlessworkflow.workflow.correlation-started.v1 | yes | Notifies about a workflow starting to correlate events |
io.serverlessworkflow.workflow.correlation-completed.v1 | yes | Notifies about a workflow completing an event correlation |
io.serverlessworkflow.workflow.cancelled.v1 | yes | Notifies about the cancellation of a workflow execution |
io.serverlessworkflow.workflow.faulted.v1 | yes | Notifies about a workflow being faulted |
io.serverlessworkflow.workflow.completed.v1 | yes | Notifies about the completion of a workflow execution |
io.serverlessworkflow.workflow.status-changed.v1 | no | Notifies about the change of a workflow’s status phase |
The
io.serverlessworkflow.workflow.status-changed.v1 event is an optional convenience event that notifies consumers solely about a workflow’s status changes, without carrying extra data. It is typically used by consumers who only need to track or report status updates. Its use is optional because it requires runtimes to publish an additional event for each necessary lifecycle change.Task Lifecycle Events
| Type | Required | Description |
|---|---|---|
io.serverlessworkflow.task.created.v1 | yes | Notifies about the creation of a task |
io.serverlessworkflow.task.started.v1 | yes | Notifies about the start of a task |
io.serverlessworkflow.task.suspended.v1 | yes | Notifies about suspending a task’s execution |
io.serverlessworkflow.task.resumed.v1 | yes | Notifies about resuming a task’s execution |
io.serverlessworkflow.task.retried.v1 | yes | Notifies about retrying a task’s execution |
io.serverlessworkflow.task.cancelled.v1 | yes | Notifies about the cancellation of a task’s execution |
io.serverlessworkflow.task.faulted.v1 | yes | Notifies about a task being faulted |
io.serverlessworkflow.task.completed.v1 | yes | Notifies about the completion of a task’s execution |
io.serverlessworkflow.task.status-changed.v1 | no | Notifies about the change of a task’s status phase |
Workflow Components
Serverless Workflow DSL allows for defining reusable components that can be referenced across the workflow:Authentications
Authentication policies define how the workflow accesses protected resources or services:Errors
Reusable error definitions that can be caught and handled:Functions
Reusable function definitions that can be called from tasks:Retries
Reusable retry policies for handling transient failures:Secrets
Sensitive information required by a workflow to securely access protected resources:Input and Output
Workflows can accept inputs and produce outputs, with optional validation and transformation:Workflow Input
JSON Schema to validate the workflow input against before execution
Runtime expression to transform the raw workflow input. Defaults to identity expression
${ . }Workflow Output
JSON Schema to validate the workflow output against before completion
Runtime expression to transform the final workflow output. Defaults to identity expression
Scheduling
Workflow scheduling allows developers to specify when and how their workflows should be executed:CRON-based Scheduling
Interval Scheduling
Delayed Restart
Event-Driven Scheduling
Event-driven scheduling defines when a new workflow instance should be created based on external events. This is different from a start
listen task, which operates within an already instantiated workflow.Complete Workflow Example
Here’s a comprehensive example demonstrating various workflow features:Best Practices
Use meaningful names
Give your workflow and tasks descriptive names that clearly indicate their purpose.
Define input/output schemas
Always validate workflow and task inputs/outputs using JSON Schema to catch errors early.
Centralize authentication
Define authentication policies in the
use section and reference them by name for better maintainability.Use reusable components
Define functions, retries, and errors in the
use section when they’re needed multiple times.Common Patterns
Sequential Processing
Conditional Execution
Parallel Processing
Related Topics
- Tasks - Learn about different task types and their properties
- Task Flow - Understand how tasks are executed and controlled
- Data Flow - Learn how data flows through workflows
- Scheduling - Deep dive into workflow scheduling options
- Events - Work with event-driven workflows