amazon-web-services - 如何在AWS步骤函数中获取迭代器/映射状态的索引

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

我正在尝试创建一个步骤函数,该函数在 map 状态下创建结果数组。 我的代码看起来像这样:

"MyIterator": {
   "Type": "Map",
   "InputPath": "$.myArray",
   "Iterator": {
     "StartAt": "Parser",
     "States": {
       "Parser": {
         "Type": "Task",
         "Resource": "${my-lambda}",
         "ResultPath": "$.Result",
         "End": true
     }
  }
}

我希望我的步骤函数遍历我提供的数组,并创建一个数组作为结果,但我不知道该怎么做。 例如,如果我的 lambda 为函数:x = x + 1 并且输入为 myArray = [0,1,2],则结果将为 结果 = [1,2,3]。我想到的是在我的 Parser 阶段访问迭代的索引。例如,如果 i 是迭代的索引,则使用 "ResultPath": "$.Result[i]"。 这在AWS步骤函数中可能吗? 我知道我可以创建另一个 lambda 来充当计数器,我只是不想添加另一个 lambda,因为对于这样的用例来说它看起来过于复杂。

我尝试使用上面的代码,但迭代似乎每次都会覆盖参数Result

最佳答案

map 项目索引可在 Context object 上找到。如$$.Map.Item.IndexItemSelector field*,它覆盖传递给每个Map迭代的值,可以引用Context对象:

"MyIterator": {
   "Type": "Map",
   "InputPath": "$.myArray",
   "Iterator": {
     "StartAt": "Parser",
     "States": {
       "Parser": {
         "Type": "Task",
         "Resource": "${my-lambda}",
         "ResultPath": "$.Result",
         "End": true
     }
  },
  "ItemSelector": {
    "ContextIndex.$": "$$.Map.Item.Index",
    "ContextValue.$": "$$.Map.Item.Value"
  },
}

使用您的示例myArray = [0,1,2],第一次迭代将收到:

{ "ContextIndex": 0, "ContextValue": 0 }

* ItemSelector 替换了已弃用的 Parameters 字段,它具有相同的功能。另请注意 ItemProcessor已替换已弃用的 Iterator 字段。不过,它们仍然有效。

关于amazon-web-services - 如何在AWS步骤函数中获取迭代器/映射状态的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75048668/

相关文章:

amazon-web-services - 如何立即在 Amazon ELB 502 响应上提供自定义错误页面?

javascript - 通过使用 aws-sdk 在 s3 Bucket 上上传图像来 react native

python - 如何以编程方式告知 AWS Step Function 执行已完成?

amazon-web-services - AWS 步骤函数 - 转换 {AWS::AccountId}::StepFunctionsYamlTransform 失败,但没有错误消息

amazon-web-services - Lambda S3 getObject流到ES接收管道

MongoDB Compass 连接问题与 Ec2 实例访问亚马逊 documentDB 集群

php - Laravel Bus Batch 待处理作业为负数

aws-api-gateway - 如何在CDK中集成Api Gateway和Step Functions?

python-3.x - botocore.exceptions.ClientError : An error occurred (ExpiredTokenException) The security token included in the request is expired

amazon-web-services - AWS 状态机 ASL : Use the Result Selector only if data is returned