All Products
Search
Document Center

CloudFlow:Inputs and outputs

Last Updated:Dec 05, 2024

CloudFlow passes data between states in a workflow. A state receives the input provided by the previous state, and returns and passes the output to the next state. This topic describes the input and output in workflows.

Background information

Data in a CloudFlow workflow flows from one state to another, which is similar to function calls in functional programming. Each state receives input and returns output. The output data is stored in the context. The data flow between states enables the automation of processes in business applications that require complex logic.

Note

The input and output must be JSON objects. The size of the input and output of a state cannot exceed 64 KiB. If the size of the input and output of a state exceeds 64 KiB, the flow fails to be executed.

Execution input and output

Execution input

You must provide information or data to start an execution. The information or data may serve as the input of one or more states. You can access the input of an execution by using the $Context.Execution.Input system expression during workflow orchestration.

For example, the logic of an online student enrollment system consists of three processes: student information collection, student information verification, and student enrollment. Basic student information is required in each process. In this case, you can pass basic student information as the input of the execution and configure the three corresponding states to access the execution input by using the system expression. The following figure shows a sample execution input.

image

Execution output

The execution output is the result of a successful execution. You cannot access the execution output by using a system variable during workflow orchestration.

State input and output

State input

During workflow orchestration, you can use the $Context.Current.Input system expression or its shorthand form $Input to access the input of the current state.

State output

During workflow orchestration, you can use the $Context.Current.Output system expression or its shorthand form $Output to access the output of the current state. Take note that the shorthand form $Output can be used only in the scopes of output constructors.

Constructors

System expressions that can be used in constructors

The following system expressions can be used in constructors:

  • $Context: The lifecycle of $Context is the same as the whole execution period of the workflow. For more information about $Context, see Data passing.

  • $Input: The lifecycle of $Input starts when the workflow enters the state and ends when the workflow exits the state. $Input is the shorthand form of $Context.Current.Input.

  • $Output: The lifecycle of $Output starts when the workflow enters the Task call response state, and ends when the workflow exits the state. $Output is the simple form of $Context.Current.Output.

Important
  • A key that ends with .$ in an input constructor, output constructor, or parameter constructor must be assigned a value in the form of an expression. Otherwise, an error occurs.

  • When you use built-in functions or system variables that start with $, append .$ to the end of the variable keys to ensure that the variables are not treated as strings.

  • An input or output constructor supports up to 10 levels of built-in function nesting.

The following figure shows the lifecycles of the $Input, $Output, and $Context system expressions, and the assignment processes in different states.

image

  • $Input is the execution input.

  • $Input= InputConstructor indicates that the result of the InputConstructor is assigned to $Input within the scope.

  • $Output is the result of the Task call. If the call is initiated in asynchronous callback mode, the value of $Output is the result of the callback.

  • $Output= OutputConstructor indicates that the result of OutputConstructor is assigned to $Output within the scope.

  • $Context remains valid throughout the execution.

InputConstructor

InputConstructor constructs the input of a workflow. The expressions of InputConstructor are different from the conditional expressions of Choice states. Conditional expressions can return only Boolean values, whereas the expressions of InputConstructor can return any value. You can use the following system variables in the InputConstructor state:

  • $Context

  • $Input

Important

When multiple input configurations exist, the input priority is in the following descending order: InputConstructor, ItemsPath, and ItemConstructor.

The following state machine definition provides an example on how to use InputConstructor:

Type: StateMachine
Name: InputConstructExample
SpecVersion: v1
StartAt: InvokeFunction
States:
  - Type: Task
    Name: InvokeFunction
    Action: FC:InvokeFunction
    TaskMode: RequestComplete
    InputConstructor:
      FieldA.$: $Context.Execution.Input
      FieldB: World
      FieldC.$: length($Context.Execution.Input)
    Parameters:
      resourceArn: >-
        acs:fc:cn-hangzhou:123456:services/helloworld.LATEST/functions/helloworld
      invocationType: Sync
      body.$: $Input 
    End: true
  • In the preceding example, the data structure of InputConstructor is Map[String]Any.

  • FieldA.$: $Context.Execution.Input indicates that the FieldA key dynamically retrieves the value from $Context.Execution.Input instead of treating it as a string.

  • FieldB: World indicates that the static string 'World' is assigned to FieldB. A value is not dynamically retrieved because the FieldB key does not end with a .$ marker.

  • FieldC.$: length($Context.Execution.Input) indicates that the built-in function length is used to retrieve the length of the value in $Context.Execution.Input and that the length is assigned to FieldC. For information about the built-in functions that can be used in constructors, see Built-in functions.

OutputConstructor

Compared with InputConstructor, OutputConstructor is executed at a different time and supports more variables in context expressions. For example, you can use only $Context or $Input in an input constructor, whereas you can use $Output in an output constructor to represent the output because a Task call already generates the output. You can use the following system variables in the OutputConstructor state:

  • $Context

  • $Input

  • $Output

The following state machine definition provides an example on how to use OutputConstructor:

Type: StateMachine
Name: OutputConstructExample
SpecVersion: v1
StartAt: InvokeFunction
States:
  - Type: Task
    Name: InvokeFunction
    Action: FC:InvokeFunction
    TaskMode: RequestComplete
    Parameters:
      resourceArn: >-
        acs:fc:cn-hangzhou:123456:services/helloworld.LATEST/functions/helloworld
      invocationType: Sync
      body.$: $Input 
    OutputConstructor:
      returnID.$: uuid()
      functionResult.$: $Output
      functionResultStr.$: jsonToString($Output)
      executionInput.$: $Context.Execution.Input
      lastOutput.$: $Input
    End: true
  • In the preceding example, the data structure of OutputConstructor is map[string]any.

  • functionResult.$ dynamically retrieves the state output, executionInput.$ dynamically retrieves the execution input, and lastOutput.$ dynamically retrieves the state input.

  • returnID.$ retrieves the unique ID, and functionResultStr.$ retrieves a string that is created from the state output.