英文:
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:
"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"
},
}
Using your example myArray = [0,1,2]
, the first iteration would receive:
{ "ContextIndex": 0, "ContextValue": 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论