3

我需要使用“AWS Step Functions”开发一个“状态机”来完成以下任务:

  1. 调用将连接到 DynamoDb 并检索行列表的 Lambda 函数。(我知道该怎么做。)
  2. 对于上一步中的每一行,我需要调用另一个 Lambda 函数,直到读取所有行。

如何在 AWS Step Functions 中执行上述第 2 步?换句话说,我如何迭代上一步的结果。

4

1 回答 1

4

这并不漂亮,但这可以通过使用 JSONPath 的切片运算符和 Sentinel 值来实现开箱即用/无需迭代器 lambda。

这是一个示例状态机:

{
  "Comment": "Example of how to iterate over an arrray of items in Step Functions",
  "StartAt": "PrepareSentinel",
  "States": {
    "PrepareSentinel": {
      "Comment": "First, prepare a temporary array-of-arrays, where the last value has a special SENTINEL value.",
      "Type": "Pass",
      "Result": [
        [
        ],
        [
          "SENTINEL"
        ]
      ],
      "ResultPath": "$.workList",
      "Next": "GetRealWork"
    },
    "GetRealWork": {
      "Comment": "Next, we'll populate the first array in the temporary array-of-arrays with our actual work. Change this from a Pass state to a Task/Activity that returns your real work.",
      "Type": "Pass",
      "Result": [
        "this",
        "stage",
        "should",
        "return",
        "your",
        "actual",
        "work",
        "array"
      ],
      "ResultPath": "$.workList[0]",
      "Next": "FlattenArrayOfArrays"
    },
    "FlattenArrayOfArrays": {
      "Comment": "Now, flatten the temporary array-of-arrays into our real work list. The SENTINEL value will be at the end.",
      "Type": "Pass",
      "InputPath": "$.workList[*][*]",
      "ResultPath": "$.workList",
      "Next": "GetNextWorkItem"
    },
    "GetNextWorkItem": {
      "Comment": "Extract the first work item from the workList into currentWorkItem.",
      "Type": "Pass",
      "InputPath": "$.workList[0]",
      "ResultPath": "$.currentWorkItem",
      "Next": "HasSentinelBeenReached"
    },
    "HasSentinelBeenReached": {
      "Comment": "Check if the currentWorkItem is the SENTINEL. If so, we're done. Otherwise, do something.",
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.currentWorkItem",
          "StringEquals": "SENTINEL",
          "Next": "Done"
        }
      ],
      "Default": "DoWork"
    },
    "DoWork": {
      "Comment": "Do real work using the currentWorkItem. Change this to be an activity/task.",
      "Type": "Pass",
      "Next": "RemoveFirstWorkItem"
    },
    "RemoveFirstWorkItem": {
      "Comment": "Use the slice operator to remove the first item from the list.",
      "Type": "Pass",
      "InputPath": "$.workList[1:]",
      "ResultPath": "$.workList",
      "Next": "GetNextWorkItem"
    },
    "Done": {
      "Type": "Succeed"
    }
  }
}
于 2018-05-11T15:58:17.660 回答