如何将 Lambda 函数输出转换为 Choice 状态的输入?

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

我刚刚开始探索 AWS 步进函数,我正在尝试创建一个状态机来调用 Lambda 函数来生成随机整数。然后,状态机使用 Choice 状态下的 Lambda 函数输出来确定执行流程。

例如,如果 Lambda 函数生成的数字小于 10,则 Choice 状态会变为 Fail 状态。如果 Lambda 函数生成的数字大于 10,则 Choice 状态变为 Pass 状态并且工作流执行完成。

错误: 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!"
    }
  }
}

错误: 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 函数代码:

exports.handler = async function(event, context) {
    const randNumber = Math.floor( 50 * Math.random() );
    return randNumber;
};

您没有在整个步骤函数中正确访问或修改状态输出,导致错误 Invalid path '$.result.latest'


您的 Lambda 有一个示例输出 29,它不在 JSON 对象中。

并且由于您没有任何状态输入,您的 ResultPath 值变得无效 - 没有状态输入,因此没有根 $ 对象可以将您的 Lambda 结果添加到其中。

您的 OutputPath 值也变得无效 - 根本没有 JSON 输出可供过滤。

因此,首先从您的状态机中删除 ResultPath & OutputPath

现在有了 29 的输出,您的 Choice 状态仍在尝试访问根本不存在的 $.result.latest

没有 JSON 对象使用 result.latest JSON 路径,所以只需通过将 Variable 从 [=19 更改为 Variable 来传递整个输出 - 数字=] 到 $.

这对我有用:

{
  "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!"
    }
  }
}