Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify multiple result path values in AWS Step Functions

Tags:

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?

like image 862
Jake T. Avatar asked Jul 06 '17 18:07

Jake T.


People also ask

What is output path in step function?

OutputPath enables you to select a portion of the state output to pass to the next state. This enables you to filter out unwanted information, and pass only the portion of JSON that you care about. If you don't specify an OutputPath the default value is $ .

How do you pass input parameters to Step Functions?

Step Functions paths use JsonPath syntax. To specify that a parameter use a path to reference a JSON node in the input, end the parameter name with . $ . For example, if you have text in your state input in a node named message , you could pass that to a parameter by referencing the input JSON with a path.

What is ResultSelector?

It is a common use case when you need to call one remote service, based on its response create a second request and combine the first response with the second response.

How do you pass data from one lambda to another?

Executing a step function Whatever the first state will receive these data as input. If the state is a Wait step it would automatically pass in the received data to the next state, but if it is a lambda you need to return it in the callback.


1 Answers

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.

like image 195
Zodman Avatar answered Oct 31 '22 10:10

Zodman