Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Step Function: Function .length() returned error in variable field in Choice state

I have a Choice state in AWS Step Function that would compare an array's length from Input and make decision to go next state.

However, the length() function, to get the array's length, returned an error:

{
"error": "States.Runtime",
"cause": "An error occurred while executing the state 'CheckItemsCountState' (entered at the event id #18). Invalid path '$.Metadata[2].Items.length()': The choice state's condition path references an invalid value."

}

The Choice state's definition is as below:

     "CheckItemsCountState":{  
         "Type": "Choice",
         "InputPath": "$",
         "OutputPath": "$",
         "Default": "NoItemsState",
         "Choices":[  
            {  
               "Variable": "$.Metadata[2].Items.length()",
               "NumericGreaterThan": 0,
               "Next": "ProcessState"
            }
         ]
      },

The state is connected some other state that returns a JSON. The JSON is as below:

{
  "Metadata": [
    {
      "foo": "name"
    },
    {
      "Status": "COMPLETED"
    },
    {
      "Items": []
    }
  ]
}

So I was trying to get the length of Items in Metadata[2] and make comparison if the value is greater than 0.

I have tried to validate the JsonPath $.Metadata[2].Items.length() in this website and it returns 0.

I am not sure if I've missed anything. I couldn't find any information in AWS Step Function's docs or example on using function in jsonpath.

I would appreciate for any help. Thank you!

like image 771
Popoi Menenet Avatar asked Jul 27 '17 04:07

Popoi Menenet


People also ask

What happens if a step function fails?

If any branch fails, because of an unhandled error or by transitioning to a Fail state, the entire Parallel state is considered to have failed and all its branches are stopped. If the error is not handled by the Parallel state itself, Step Functions stops the execution with an error.

How long step function can wait?

High Execution Time: Step Functions has one year as max execution time so if some of the tasks of the workflow are high (more than 15 minutes), they can be run on ECS or EC2 or as an Activity hosted outside of AWS.


1 Answers

Step Functions do not allow you to use functions to get values. From the Choice Rules documentation:

For each of these operators, the corresponding value must be of the appropriate type: string, number, Boolean, or timestamp.

To do what you're asking, you would need to get the array length in the previous function and return it as part of the output.

{
  "Metadata": [
    {
      "foo": "name"
    },
    {
      "Status": "COMPLETED"
    },
    {
      "Items": [],
      "ItemsCount": 0
    }
  ]
}

Then in the Step Function Choice step:

"CheckItemsCountState":{  
    "Type": "Choice",
    "InputPath": "$",
    "OutputPath": "$",
    "Default": "NoItemsState",
    "Choices":[  
        {  
            "Variable": "$.Metadata[2].ItemsCount",
            "NumericGreaterThan": 0,
            "Next": "ProcessState"
        }
    ]
},
like image 63
John Veldboom Avatar answered Nov 13 '22 05:11

John Veldboom