How can I get the results from previous Map
Iterations
in the next iteration when using MaxConcurrency: 1
in Amazon Step Functions?
Here's an example of the code I have
{
"StartAt": "UploadUsers",
"States": {
"UploadUsers": {
"Type": "Map",
"MaxConcurrency": 1,
"ItemsPath": "$.data.users",
"Parameters": {
"data.$": "$$.Map.Item.Value.data",
"friends.$": "$.?????? Get created users ids"
},
"Iterator": {
"StartAt": "UploadUser",
"States": {
"UploadUser": {
"End": true,
"Parameters": {
"FunctionName": "${FnUploadUser}",
"Payload": {
"data.$": "$.user_data",
"friends.$": "$.??????"
}
},
"Resource": "arn:aws:states:::lambda:invoke.waitForTaskToken",
"ResultPath": "$.data. ???",
"Type": "Task"
}
}
},
"End": true,
"ResultPath": "$.data.UploadUsers",
"ResultSelector": {
"result.$": "$"
}
}
}
}
Suppose FnUploadUser
is a lambda that returns the id of the created user.
And I want to get the ids of the previously created users and use that value for the next user I'm about to create.
CodePudding user response:
You can't. Map
State iterations don't share state. Two workarounds:
(1) Manage the shared state externally: Each Map
iteration writes and reads from, say, a DynamoDB table.
(2) Refactor to a "for" loop and keep the shared state in the execution output.
- Instead of using
Map
, insert aChoice
State (afterUploadUser
) that checks for a "done" condition. If "done", finish, else loop back toUploadUser
. UploadUser
accepts theuser_data
array as input. It appends its output to, say, theuploaded
output array.- Each
UploadUser
iteration identifies the nextuser_data
item by comparing it to theuploaded
array. The iteration that processes the last item can also outputdone: true
to signal toChoice
that work is done. - The
Choice
State loops back toUploadUser
while there are more to process (i.e. whiledone
is not present).
There are other ways to build steps 2-3. For instance, you could add next_item
and total_items
keys on the output to keep track of progress. The important point is that Choice
loops until an exit condition is met.