All Products
Search
Document Center

CloudFlow:Integrate CloudFlow with Function Compute

Last Updated:Mar 18, 2024

CloudFlow allows you to invoke and integrate Alibaba Cloud services by using the Flow Definition Language (FDL). This topic describes how CloudFlow is integrated with Function Compute and invokes functions.

Background information

CloudFlow describes a business flow based on the states of tasks and models of state machines, and uses API call parameters that you specify to complete each step of the flow.

If you want to add functions to CloudFlow and invoke functions of CloudFlow, you must follow orchestration specifications. The following sections provide a sample procedure on how to add a function to CloudFlow and then invoke the function.

Step 1: Select a state type

Add a task-type state to the flow, and then orchestrate Function Compute based on the definition of the task type.

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: true

Step 2: Configure invocation parameters

Function invocations in Function Compute involve the following major parameters: the Alibaba Cloud Resource Name (ARN) that identifies the function, the function invocation mode, and the event parameter. The following table describes the parameters.

Note

According to the Spec specification, the parameters follow the camel case naming conventions and start with a lowercase letter.

Parameter

Required

Type

Description

resourceArn

Yes

String.

The ARN of the function, which is used to identify the function.

invocationType

Yes

Sync or Async.

The function invocation mode.

body

No

JSON object or string, which is serialized into a byte array before being passed to the invoked function.

The event parameter, which is the input parameter of the function execution process.

The following code shows an example.

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: true

Step 3: Set an integration mode

Task-type states support the TaskMode attribute, which defines how the current state handles the API call. The integration of CloudFlow and Function Compute supports the following integration modes.

  • RequestComplete

    In this mode, after the system responds to the request and returns the result, the flow advances to the next state. This mode is effective for synchronous function invocations, asynchronous function invocations, and asynchronous task invocations.

  • WaitForSystemCallback

    In this mode, after the system sends a request for function invocations, the function system sends a system callback notification. After the workflow receives the system callback notification, the workflow executes the next step. This mode is effective only for asynchronous function invocations and asynchronous task invocations.

  • WaitForCustomCallback

    After a function invocation request is completed, the workflow continues to wait. The workflow does not advance until the user program notifies the API to send a notification by using the callback that is provided by CloudFlow and CloudFlow receives the callback notification. This mode is effective only for asynchronous function invocations and asynchronous task invocations.

For more information about function invocations, see Synchronous invocations, Asynchronous invocations, and Asynchronous task management.

StartAt: an example of synchronous function invocation.
States:
  - Name: an example of synchronous function invocation.
    Type: Task
    TaskMode: RequestComplete
    Action: FC::InvokeFunction
    Parameters:
      resourceArn: acs:fc:::services/myService1.LATEST/functions/myFunction1
      invocationType: Sync
      body: myEvent
    Next: an example of asynchronous function invocation.
  - Name: an example of asynchronous function invocation.
    Type: Task
    TaskMode: WaitForUserCallback
    Action: FC::InvokeFunction
    Parameters:
      resourceArn: acs:fc:::services/myService1.LATEST/functions/myFunction1
      invocationType: Async
      body:
      	payloadEvent: myEvent
        callbackToken: $Context.Current.TaskToken
    End: true

You must add the callbackToken custom parameter to the body parameter and reference the $Context.Current.TaskToken expression. The expression specifies the token attribute that must be used when the workflow calls back the task execution result.

Step 4: Construct invocation parameters

In actual flow orchestration, you must construct the parameters that are required by the current function invocation based on the context of the workflow and the result of the previous state. The following code provides an example on how to construct invocation parameters.

  • You can use the $Context and $Input constants reserved by the Spec specification to access data in the flow.

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: true
    Description: null

Step 5: Handle the invocation result

You invoke states to complete the entire flow. In a function invocation scenario, the workflow returns the execution result after the workflow completes the invocation. You can use the returned result as a conditional statement of the flow or as an input of the next state. According to the InvokeFunction API definition of Function Compute, if the value of the Type response parameter of the function is Byte, the actual data type is Byte. The return value of the function can be a string-type JSON object, JSON array, or of a basic data type. After the workflow receives the returned result, the workflow encapsulates the result into a JSON object. The following code shows the result encapsulation structure in the workflow.

{
	"Body": function invocation result.
}

You can use the JSONPath of $Output.Body in the description of the workflow to access the result returned by the function. If the returned value is of the JSONObject type, you can call the built-in function in the workflow to deserialize the returned value. Then, you can access the members in the returned value based on the operation of JSINObject.

Type: Workflow
Name: shuhe-demo
SpecVersion: v1
Description: " "
Timeout: null
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: true

Common error definitions

  • FC.ResourceThrottled: Your functions are throttled due to high concurrency. All your functions are controlled by a total concurrency value. CloudFlow invokes Function Compute when CloudFlow executes the task node. The total concurrency value is the sum of the concurrency value in function invocations and the concurrency values of other invocation methods. You can apply to modify the value.

  • FC.ResourceExhausted: Your functions are throttled due to insufficient resources. Contact us when errors of this type occur.

  • FC.InternalServerError: A system error occurs on Function Compute. Execute the flow again.

  • FC.AccessDenied: Your access request is denied because your Alibaba Cloud account does not have the required permissions.

  • FC.InvalidArgument: The parameter is invalid.

  • FC.EntityTooLarge: The specified input parameter value exceeds the valid value range.

Note: For more information about error types in Function Compute, see Error codes.