Home > OS >  How to transform Lambda function output as input for Choice state?
How to transform Lambda function output as input for Choice state?

Time:05-01

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;
};

CodePudding user response:

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!"
    }
  }
}
  • Related