CloudFlow orchestrates serverless workflows as state machines defined in Flow Definition Languages (FDLs). To call a Function Compute function from a workflow, add a Task state with the FC:InvokeFunction action. The flow engine invokes the function, handles the response based on the integration mode you choose, and advances to the next state.
How it works
A CloudFlow flow chains states together. Each Task state can invoke an Alibaba Cloud service. For Function Compute integration, the flow follows this sequence:
A Task state triggers the
FC:InvokeFunctionaction.The flow engine calls the target function with the parameters you specify (ARN, invocation type, and payload).
Based on the
TaskModesetting, the flow either waits for the API response, waits for a system callback, or waits for a custom callback from your code.The flow wraps or passes through the function's return value, then advances to the next state.
Key concepts
| Concept | Description |
|---|---|
| Task state | A state that performs work by calling an external service. |
FC:InvokeFunction | The action identifier that tells CloudFlow to invoke a Function Compute function. |
TaskMode | Controls how the flow handles the response: wait for the API reply, wait for a system callback, or wait for a custom callback. |
resourceArn | The Alibaba Cloud Resource Name (ARN) that uniquely identifies the target function. |
| FDL expressions | Built-in expressions ($Input, $Output, $Context) for accessing data within a flow. |
Define a Task state
Add a Task state to your flow definition. Set Action to FC:InvokeFunction and provide the required parameters under Parameters:
Type: StateMachine
Name: MyWorkflow
SpecVersion: v1
StartAt: an example of function invocation
States:
- Name: an example of function invocation
Type: Task
TaskMode: RequestComplete
Action: FC:InvokeFunction
Parameters:
resourceArn: xxx
invocationType: xxx
body: xxx
End: trueConfigure invocation parameters
Three parameters control how the function is called:
| Parameter | Required | Type | Description |
|---|---|---|---|
resourceArn | Yes | String | ARN that identifies the target function. Example: acs:fc:::services/myService1.LATEST/functions/myFunction1 |
invocationType | Yes | Sync or Async | Synchronous or asynchronous invocation. |
body | No | JSON object or string (serialized to a byte array before being passed to the function) | Event payload passed as the function's input parameter. |
Per the Spec specification, parameter names use camelCase and start with a lowercase letter.
The following example defines two states -- one synchronous invocation followed by one asynchronous invocation:
Type: StateMachine
Name: MyWorkflow
SpecVersion: v1
StartAt: an example of synchronous function invocation
States:
- Name: an example of synchronous function invocation
Type: Task
Action: FC:InvokeFunction
TaskMode: RequestComplete
Parameters:
resourceArn: acs:fc:::services/myService1.LATEST/functions/myFunction1
invocationType: Sync
body:
testKey: the content of the testKey parameter in the event of the function
Next: an example of asynchronous function invocation
- Name: an example of asynchronous function invocation
Type: Task
Action: FC:InvokeFunction
TaskMode: RequestComplete
Parameters:
resourceArn: acs:fc:::services/myService2.LATEST/functions/myFunction2
invocationType: Async
body:
key1: value1
key2: value2
End: trueChoose an integration mode
The TaskMode attribute controls how the flow handles the API call after invoking the function. Three modes are available:
| Mode | Supported invocation types | When to use |
|---|---|---|
RequestComplete | Sync, Async, Async task | The flow waits for the API response, then proceeds. |
WaitForSystemCallback | Async, Async task only | Use when Function Compute sends a system-level callback after the function finishes. |
WaitForCustomCallback | Async, Async task only | Use when your own code determines when the flow should proceed. |
RequestComplete
The flow invokes the function and waits for the API response. After the system responds and returns the result, the flow advances to the next state. This mode works with synchronous invocations, asynchronous invocations, and asynchronous task invocations.
For more information, see RequestComplete.
WaitForSystemCallback
The flow sends the invocation request, then waits for Function Compute to send a system callback notification. The flow advances only after receiving that callback. This mode works with asynchronous invocations and asynchronous task invocations only.
For more information, see WaitForSystemCallback.
WaitForCustomCallback
The flow invokes the function, then pauses. It advances only when your program calls the CloudFlow callback API to report the task result. This mode works with asynchronous invocations and asynchronous task invocations only.
To use this mode, add a callbackToken field to the body parameter and reference the $Context.Current.TaskToken expression. This token is the credential your program must include when reporting the task result back to the flow.
Type: StateMachine
Name: MyWorkflow
SpecVersion: v1
StartAt: invoke-with-callback
States:
- Name: invoke-with-callback
Type: Task
Action: FC:InvokeFunction
TaskMode: WaitForCustomCallback
Parameters:
resourceArn: acs:fc:::services/myService1.LATEST/functions/myFunction1
invocationType: Async
body:
callbackToken.$: $Context.Current.TaskToken
key1: value1
End: trueIn this example, the flow passes the task token to the function via callbackToken. The function (or a downstream process) calls the ReportTaskSucceeded or ReportTaskFailed API with this token to notify CloudFlow of the result.
For more information, see WaitForCustomCallback.
For details on function invocation types, see Synchronous invocations, Asynchronous invocations, and Asynchronous task management.
Construct dynamic parameters
In most workflows, function inputs depend on the workflow context or the output of a previous state rather than static values. Use the $Context and $Input reserved expressions to reference dynamic data.
Append .$ to a parameter name to indicate that its value is an FDL expression rather than a literal:
Type: StateMachine
Name: MyWorkflow
SpecVersion: v1
StartAt: an example of function invocation
States:
- Name: an example of function invocation
Type: Task
TaskMode: RequestComplete
Action: FC:InvokeFunction
Parameters:
resourceArn: acs:fc:::services/myService1.LATEST/functions/myFunction1
invocationType: Sync
body:
argument1.$: $Input.input.BucketName
argument2.$: $Input.input.TaskId
End: trueIn this example, argument1 receives the value of $Input.input.BucketName and argument2 receives $Input.input.TaskId at runtime.
Handle invocation results
How the flow wraps the function's return value depends on the integration mode.
According to the InvokeFunction API definition, if the value of the Type response parameter of the function is Byte, the actual data type is Byte. The function return value can be a string, JSON object, JSON array, or a basic data type.
RequestComplete mode
The flow wraps the function's return value in a JSON object with a Body field:
{
"Body": "function invocation result"
}Access the return value with $Output.Body in subsequent state definitions. If the return value is a JSON object, deserialize it with the workflow's built-in function, then access its members as a standard JSON object.
The following example invokes a function, then uses a Choice state to branch based on a value from the function's output:
Type: StateMachine
Name: MyWorkflow
SpecVersion: v1
Description: ' '
StartAt: Example 1 of function invocation
States:
- Name: Example 1 of function invocation
Type: Task
TaskMode: RequestComplete
Action: FC:InvokeFunction
Parameters:
resourceArn: acs:fc:::services/myService1.LATEST/functions/myFunction1
invocationType: Sync
body:
argument1.$: $Input.Records
argument2.$: $Input.BucketName
Next: conditional statement
- Type: Choice
Name: conditional statement
Branches:
- Condition: $Input.ObjectSize >= $Input.Threshold
Next: Example 2 of function invocation
Default: No action is performed.
- Type: Pass
Name: No action is performed.
End: true
- Name: Example 2 of function invocation
Type: Task
TaskMode: RequestComplete
Action: FC:InvokeFunction
Parameters:
resourceArn: acs:fc:::services/myService2.LATEST/functions/myFunction2
invocationType: Sync
body:
argument1.$: $Input.BucketName
argument2.$: $Input.TaskId
End: trueWaitForSystemCallback and WaitForCustomCallback modes
In these modes, the external system calls the ReportTaskSucceeded or ReportTaskFailed API to report the result. The flow passes the result through without wrapping it. For example, if the function returns {"message": "hello"}, the result in the flow is:
{
"message": "hello"
}Access the return value directly with the $Output expression.
Common errors
| Error code | Cause | Action |
|---|---|---|
FC.ResourceThrottled | Functions are throttled because total concurrent invocations (across CloudFlow and all other invocation sources) exceed the concurrency limit. CloudFlow synchronously invokes Function Compute when CloudFlow executes Task states. | Request a concurrency limit increase. |
FC.ResourceExhausted | Functions are throttled because of insufficient resources. | Contact us. |
FC.InternalServerError | A system error occurred in Function Compute. | Re-execute the flow. |
FC.AccessDenied | The Alibaba Cloud account lacks the required permissions. | Grant the necessary permissions and retry. |
FC.InvalidArgument | A parameter value is invalid. | Verify parameter values against the specification. |
FC.EntityTooLarge | An input parameter value exceeds the allowed size. | Reduce the input payload size. |
For other Function Compute error codes, see Error codes.