如何循环遍历 json 输入 - aws 步骤函数/状态机

War*_*ard 6 json amazon-web-services aws-step-functions

是否可以在 aws 步骤函数中创建循环并循环遍历 json 输入数组?

我有一个函数generateEmails可以创建包含n多个对象的数组:

{
  "emails": [
    {
      "to": [
        "willow1@aaa.co.uk"
      ]
    },
    {
      "to": [
        "willow2@aaa.co.uk"
      ]
    },    {
      "to": [
        "willow3@aaa.co.uk"
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

现在我想sendEmail为电子邮件数组中的每个对象调用下一个函数,如下所示:

{
  "email": {
    "to": [
      "willow@aaa.co.uk"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

步骤功能代码:

{
  "Comment": "A state machine that prepares and sends confirmation email ",
  "StartAt": "generateEmails",
  "States": {
    "generateEmails": {
      "Type": "Task",
      "Resource": "arn:aws:lambda::prepare-confirmation-email",
      "Next": "sendEmail"
    },
    "sendEmail": {
      "Type": "Task",
      "Resource": "arn:aws:lambda::function:template-service",
      "End" : true
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这有可能实现吗?

谢谢!

小智 0

是的,Step Functions 映射状态使这一切变得简单。

https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-map-state.html

Map 允许您对数组中的每个项目运行相同的操作集。如果将 MaxConcurrency 字段设置为大于 1,那么它将并行执行这些操作。或者您可以设置为 1,它将按顺序迭代。

对于您描述的场景,项目的数量可能意味着“内联”地图可以正常工作。但如果该列表更大并且您想要扇出更高的并发性,最近推出的分布式映射功能将允许您这样做。

https://aws.amazon.com/blogs/aws/step-functions-distributed-map-a-serverless-solution-for-large-scale-parallel-data-processing/