如何在AWS Step Functions中获取迭代器/映射状态的索引?

huangapple go评论63阅读模式
英文:

How can I get the index of an iterator/map state in AWS step function

问题

我正在尝试创建一个步骤函数,在映射状态下创建一个结果数组。
我的代码大致如下:

"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],那么结果将是 Result = [1,2,3]。我考虑过在我的 Parser 阶段中访问迭代的索引。例如,如果 i 是迭代的索引,可以使用 "ResultPath": "$.Result[i]"。在 AWS 步骤函数中是否可能实现这一点?
我知道我可以创建另一个 lambda 函数作为计数器,但我真的不想为这样一个简单的用例添加另一个函数,因为这看起来过于复杂。

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

英文:

I'm trying to create a step function that creates an array of results in a map state.
My code looks something like that:

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

I want my step function to go through the array I provided, and create an array as the result, but I'm not sure how to do it.
For example, if my lambda will be the function: x = x + 1 and the input is myArray = [0,1,2], the result will be Result = [1,2,3]. Something I thought of is to access the index of the iteration in my Parser stage. For example, use "ResultPath": "$.Result[i]" if i is the index of the iteration.
Is that possible in AWS step functions?
I know I can create another lambda that acts as a counter, I just don't really want to add another one as it looks way over complicated for such a usecase.

I tried to use the above code, but the iteration seemed to override the parameter Result each time.

答案1

得分: 3

Map项目索引在上下文对象中作为$$.Map.Item.Index可用。ItemSelector字段*可以引用上下文对象,它会覆盖传递给每个Map迭代的值:

"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字段。它们仍然可以使用。

英文:

The Map item index is available on the Context object as $$.Map.Item.Index. The ItemSelector field<sup>*</sup>, which overrides the values passed to each Map iteration, can reference the Context object:

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

Using your example myArray = [0,1,2], the first iteration would receive:

{ &quot;ContextIndex&quot;: 0, &quot;ContextValue&quot;: 0 }

* ItemSelector replaces the deprecated Parameters field, which did the same thing. Note also that ItemProcessor has replaced the deprecated Iterator field. They all still work, though.

huangapple
  • 本文由 发表于 2023年1月8日 22:44:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定