1

I have just started exploring AWS step functions & I'm trying to create a state machine that invokes a Lambda function to generate random integers. The state machine then uses the Lambda function output in a Choice state to determine the execution flow.

For example, if the Lambda function generates a number < 10, the Choice state moves to a Fail state. If the Lambda function generates a number > 10, the Choice state moves to a Pass state and workflow execution completes.

Error: The choice state's condition path references an invalid value.

{
  "Comment": "An example of input output processing",
  "StartAt": "getScore",
  "States": {
    "getScore": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:***:function:random-num",
      "ResultPath": "$.result",
      "OutputPath": "$.result",
      "Retry": [
        {
          "ErrorEquals": [
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException"
          ],
          "IntervalSeconds": 2,
          "MaxAttempts": 3,
          "BackoffRate": 2
        }
      ],
      "Next": "checkScoreValue"
    },
    "checkScoreValue": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.result.latest",
          "NumericLessThan": 40,
          "Next": "Fail"
        },
        {
          "Variable": "$.result.latest",
          "NumericGreaterThan": 41,
          "Next": "Pass"
        }
      ],
      "Default": "Pass"
    },
    "Pass": {
      "Type": "Pass",
      "Result": "You have passed",
      "End": true
    },
    "Fail": {
      "Type": "Fail",
      "Cause": "You have failed!"
    }
  }
}

Error: An error occurred while executing the state 'checkScoreValue' (entered at the event id #7). Invalid path '$.result.latest': The choice state's condition path references an invalid value.

Lambda function code:

exports.handler = async function(event, context) {
    const randNumber = Math.floor( 50 * Math.random() );
    return randNumber;
};
1
  • 1
    Welcome to Stack Overflow! What does your function return if you test the function via the console? Can you please add the output to your question please? Commented Apr 30, 2022 at 3:30

1 Answer 1

2

You're not accessing or modifying the state output correctly throughout this step function, resulting in the error of Invalid path '$.result.latest'.


Your Lambda has a sample output of 29, which isn't within a JSON object.

And since you don't have any state input, your ResultPath value becomes invalid - there is no state input, and thus no root $ object to add your Lambda result into.

Your OutputPath value also becomes invalid - there is no JSON output at all to filter from.

So, first remove ResultPath & OutputPath from your state machine.

Now with the output of 29, your Choice state is still trying to access $.result.latest which simply does not exist.

There is no JSON object to use the result.latest JSON path so just pass the entire output - the number - through by changing Variable from $.result.latest to $.

This works for me:

{
  "Comment": "An example of input output processing",
  "StartAt": "getScore",
  "States": {
    "getScore": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:***:function:random-num",
      "Retry": [
        {
          "ErrorEquals": [
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException"
          ],
          "IntervalSeconds": 2,
          "MaxAttempts": 3,
          "BackoffRate": 2
        }
      ],
      "Next": "checkScoreValue"
    },
    "checkScoreValue": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$",
          "NumericLessThan": 40,
          "Next": "Fail"
        },
        {
          "Variable": "$",
          "NumericGreaterThan": 41,
          "Next": "Pass"
        }
      ],
      "Default": "Pass"
    },
    "Pass": {
      "Type": "Pass",
      "Result": "You have passed",
      "End": true
    },
    "Fail": {
      "Type": "Fail",
      "Cause": "You have failed!"
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Ermiya. Thank you so much for the detailed response. I am now able to run my state machine, and also understand about input output processing in Step Functions a bit more. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.