How to specify multiple result path values in AWS Step Functions

Jake T. picture Jake T. · Jul 6, 2017 · Viewed 15.4k times · Source

I have a AWS Step Function State formatted as follows:

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

I want to add a second value but can't find out how anywhere. None of the AWS examples display multiple ResultPath values being added to the output.

Would I just add a comma between them?

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

Or is there a better way to format these?

Answer

Zodman picture Zodman · Feb 1, 2018

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.