All Products
Search
Document Center

CloudFlow:Data passing

Last Updated:Oct 12, 2024

Data passing among multiple states in a flow is similar to data passing in a functional programming language. States in CloudFlow are similar to functions in programming. The states accept inputs, return outputs, and save the outputs in the context. This topic describes the overview and usage examples of data passing.

Overview

You can combine and nest states in CloudFlow. The data in the runtime environment of a state is called context. The data can be classified into the following categories:

  • Flow data: includes the name and region of the flow, the names of the states that make up the flow, and the Resource Access Management (RAM) role that is used to execute the flow.

  • Execution data: includes the name and input of the execution, the input and output of the current state, the token used for callback, the error information generated during the execution, and the number of retries for error handling.

Data access

You must use the $Context expression provided by the system to access the data related to flows and flow execution, regardless of static data or dynamic data. Context contains the following content:

{
  "Execution": {
    "Name": "String",
    "Input": {},
    "RoleArn": "String"
  },
  "Current": {
    "Name": "String",
    "Input": {},
    "Output": {},
    "Error": {
      "Code": "String",
      "Detail": "String"
    },
    "RetryCount": "Number",
    "TaskToken": "String"
  }
}

Context can be used to construct data in the following scenarios:

  • Input constructors and output constructors. For more information, see Inputs and outputs and State OutputConstructor.

  • The ItemsPath field in Map states. Example: $Context.Current.Input.

  • The Condition field in Choice states. Example: $Context.Current.Input.Size>=1024.

  • Error handling information determination. Examples: $Context.Current.Error.Code and $Context.Current.Error.Detail.

  • Passing of task callback tokens. Example: $Context.Current.TaskToken.

CloudFlow provides the frequently used shortcuts $Input and $Output. You can use the shortcuts to quickly access the input and output of the current node.

  • $Input is equivalent to $Context.Current.Input.

  • $Output is equivalent to $Context.Current.Output.

Data passing

Different states have different input and output logic. If input and output constructors are not defined, states have the following input and output logic:

  • Pass, Succeed, Fail, and Wait: These states accept inputs and use the original inputs that are not processed as outputs.

  • Choice: This state only accepts inputs and passes the inputs to the destination state without returning outputs. For example, the Choice state may pass inputs to the Default branch. The Choice state may also pass inputs to a specific branch to determine whether the required conditions are met. If the required conditions are met, the Choice state passes the inputs to the next state.

  • Parallel: The system makes deep copies of an input and passes the input to multiple branches at the same time. The outputs of the state are in the Map[String]Any format. The key in Map[String]Any is the implicit name called Branch N of a parallel branch. The value in Map[String]Any is the actual processing result of a parallel branch.

  • Map: The system determines whether an input is an array or a Map[String]Any. If the input of the Map state is not an array and you do not specify the content to be iterated by using the ItemsPath parameter, the system automatically obtains the values of the Map state as an iterative array. The system iterates the array. Array elements are used as inputs to the iteration processor. The outputs of the Map state are in the Map[String]Any format. The key in Map[String]Any is the Items string. The value in Map[String]Any is an array that consists of multiple iteration results.

  • Task: This state accepts input data from the previous state or an external data source, executes specific business logic in a task script, uses the $Context expression to access and manipulate data, and then passes the task execution result as output data to subsequent states or stores it in an external system.

Example

Type: StateMachine
Name: DataTransferExample
SpecVersion: v1
StartAt: Step1
States:
  - Type: Pass
    Name: Step1
    Next: Step2
  - Type: Parallel
    Name: Step2
    InputConstructor:
      FieldA: 123
    Branches:
      - StartAt: Pass1
        States:
          - Type: Pass
            Name: Pass1
            End: true
      - StartAt: Pass2
        States:
          - Type: Pass
            Name: Pass2
            End: true
    Next: Step3
  - Type: Pass
    Name: Step3
    End: true

In the preceding example, InputConstructor is used in a Parallel state named Step2 to construct a JSON object that contains the FieldA property. The value of the FieldA property is 123. By default, the input is passed to ParallelBranch #0 and ParallelBranch #1. For example, in the ParallelBranch #0 branch, the input is passed to Pass1. The following code provides an example of the context data of Pass1.

{
  "Execution": {
    "Name": "xxxx-xxxx-xxxx-xxxx",
    "Input": {},
    "RoleArn": "xxxx"
  },
  "Current": {
    "Name": "Pass1",
    "Input": {
      "FieldA": 123
    },
    "Output": {
      "FieldA": 123
    },
    "Error": null,
    "RetryCount": 0
  }
}