amazon-web-services - 在 AWS Step Functions 中迭代先前状态的结果

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

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

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

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

最佳答案

这不是很漂亮,但是这可以通过使用 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"
    }
  }
}

关于amazon-web-services - 在 AWS Step Functions 中迭代先前状态的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48512745/

相关文章:

amazon-web-services - 使用查询编辑器在 Amazon Redshift 上创建数据库

node.js - 使用 AWS 部署 MS Bot Framework

aws-lambda - Lambda 函数完成后 Amazon Cloudformation 堆栈挂起

java - 使用 AmazonSNSClient 发送短信时的授权

ruby-on-rails - 使用 Rails 应用程序下载 S3 文件

ios - 如何加快亚马逊云前端的图像加载速度

amazon-web-services - 如何添加策略以使用 lambda 函数访问 secret AWS SAM

amazon-web-services - 自动为所有 AWS 账户创建相同的策略

python - 在 AWS Lambda 上使用来自 Python 的 NodeJS 4 脚本

java - 使用变量的值作为枚举值