简体   繁体   中英

How to specify multiple result path values in AWS Step Functions

I have a AWS Step Function State formatted as follows:

"MyState": {
  "Type": "Task",
  "Resource": "<MyLambdaARN>",
  "ResultPath": "$.value1"
  "Next": "NextState"
}

Let's answer this straight up: you can't specify multiple ResultPath values, because it doesn't make sense. Amazon does do a pretty bad job of explaining how this works, so I understand why this is confusing.

You can, however, return multiple result values from a State in your State Machine.

General Details

The input to any State is a JSON object. The output of the State is a JSON object.

ResultPath directs the State Machine what to do with the output (result) of the State. Without specifying ResultPath , it defaults to $ which means all the input to the State is lost, replaced by the output of the State.

If you want to allow data from the input JSON to pass through your State, you specify a ResultPath to describe a property to add/overwrite on the input JSON to pass to the next State.

Your scenario

In your case, $.value1 means the output JSON of your State is the input JSON with a new/overwritten property value1 containing the output JSON of your lambda.

If you want multiple values in your output, your lambda should return a JSON object containing the multiple values, which will be the value of the value1 property.

If you don't care about allowing input values passing through your State, leave the ResultPath as the default $ by omitting it. The output JSON containing your multiple values will be the input to the next State.

Support scenario

Here's a simple State machine I use to play with the inputs and outputs:

{
  "StartAt": "State1",
  "States": {
    "State1": {
      "Type": "Pass",
      "Result": { "Value1": "Yoyo", "Value2": 1 },
      "ResultPath": "$.Result",
      "Next": "State2"
    },
    "State2": {
      "Type": "Pass",
      "Result": { "Value2": 5 },
      "ResultPath": "$.Result",
      "Next": "State3"
    },
    "State3": {
      "Type": "Pass",
      "Result": "Done",
      "End": true
    }
  }    
}

Execute this with the following input:

{
  "Input 1": 10000, 
  "Input 2": "YOLO", 
  "Input 3": true
}

Examine the inputs and outputs of each Stage. You should observe the following:

  • The input is passed all the way through, because the ResultPath always directs output to a Result property of the input.
  • The output of State1 is overwritten by the Output of State2 . The net effect is Result.Value1 disappears and Result.Value2 is "updated".

Hopefully this clarifies how to use ResultPath effectively.

You cannot specify several values in ResultPath, because ResultPath defines the path of your result value in the json. The close analogy for ResultPath is a return value of a function, as your step can return only 1 value it should be put into 1 node in the resulting json.

If you have an input json

{
  "myValue": "value1",
  "myArray": [1,2,3]
}

And define your ResultPath as $.myResult the overall resulting json will be

{
  "myValue": "value1",
  "myArray": [1,2,3],
  "myResult": "result"
}

Now you can truncate this json to pass only part of it to the next step in your function using OutputPath (eg OutputPath: "$.myResult")

InputPath and OutputPath can have several nodes in their definition, but ResultPath should always have only 1 node.

I think it would be like the following, but I haven't actually done this so I can't say for sure.

"ResultPath": {

                 "var1" : "$.value1",
                 "var2" : "$.value2"
 },

=====

After looking further into this, I am convinced that there is no direct way to do what you want to do. Here is a way that could give you the results that you want.

1) You would omit the InputPath, OutputPath, and ResultPath from your step. This would mean that all of $. would be passed in as an input to your step function and that all of the output from the lambda function would be stored as $. In the lambda function you could set the results fields to be whatever you want them to be. The lambda function must return the modified input as it output.

好像今天可以通过使用ResultsSelector来完成。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM