迭代AWS步骤函数中先前状态的结果

时间:2018-01-30 01:55:39

标签: amazon-web-services aws-lambda aws-sdk aws-step-functions

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

  1. 调用将连接到DynamoDb& amp;的Lambda函数。检索行列表。 (我知道怎么做。)
  2. 对于上一步中的每一行,我需要调用另一个Lambda函数,直到读取所有行。
  3. 如何在AWS Step Functions中执行上面的步骤2?换句话说,我如何迭代上一步的结果。

1 个答案:

答案 0 :(得分:4)

它并不漂亮,但是这可以通过使用JSONPath的 slice 运算符和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"
    }
  }
}