Parallel States Merge the output in Step Function
Solution 1
A parallel task always outputs an array (containing one entry per branch).
You can tell AWS step functions to append the output into new (or existing) property in the original input with "ResultPath": "$.ParallelOut"
in your parallel state definition, but this is not what you seem to be trying to achieve.
To merge the output of parallel task, you can leverage the "Type": "Pass"
state to define transformations to apply to the JSON document.
For example, in the state machine below, I'm transforming a JSON array...
[
{
"One": 1,
"Two": 2
},
{
"Foo": "Bar",
"Hello": "World"
}
]
...into a few properties
{
"Hello": "World",
"One": 1,
"Foo": "Bar",
"Two": 2
}
{
"Comment": "How to convert an array into properties",
"StartAt": "warm-up",
"States": {
"warm-up": {
"Type": "Parallel",
"Next": "array-to-properties",
"Branches": [
{
"StartAt": "numbers",
"States": {
"numbers": {
"Type": "Pass",
"Result": {
"One": 1,
"Two" : 2
},
"End": true
}
}
},
{
"StartAt": "words",
"States": {
"words": {
"Type": "Pass",
"Result": {
"Foo": "Bar",
"Hello": "World"
},
"End": true
}
}
}
]
},
"array-to-properties": {
"Type": "Pass",
"Parameters": {
"One.$": "$[0].One",
"Two.$": "$[0].Two",
"Foo.$": "$[1].Foo",
"Hello.$": "$[1].Hello"
},
"End": true
}
}
}
Solution 2
It is possible as opposed diagram below
The parallel state should look like this
"MyParallelState": {
"Type": "Parallel",
"InputPath": "$",
"OutputPath": "$",
"ResultPath": "$.ParallelResultPath",
"Next": "SetCartCompleteStatusState",
"Branches": [
{
"StartAt": "UpdateMonthlyUsageState",
"States": {
"UpdateMonthlyUsageState": {
"Type": "Task",
"InputPath": "$",
"OutputPath": "$",
"ResultPath": "$.UpdateMonthlyUsageResultPath",
"Resource": "LambdaARN",
"End": true
}
}
},
{
"StartAt": "QueueTaxInvoiceState",
"States": {
"QueueTaxInvoiceState": {
"Type": "Task",
"InputPath": "$",
"OutputPath": "$",
"ResultPath": "$.QueueTaxInvoiceResultPath",
"Resource": "LambdaARN",
"End": true
}
}
}
The output of MyParallelState
will be populated as in array, from each state in the Parallel state
. They are populated within ParallelResultPath
object and will be passed into the Next state
{
"ParallelResultPath": [
{
"UpdateMonthlyUsageResultPath": Some Output
},
{
"QueueTaxInvoiceResultPath": Some Output
}
]
}
Solution 3
Your diagram is technically wrong because no state can set multiple states to its Next
task. You cannot start State Machine as StartAt
by providing multiple State names. Also, even if it was possible I don't see any point why would you want to run two parallel states as opposed to one parallel state with all the sub states that you would split into two.
Related videos on Youtube
hatellla
I am a software developer. I love open source software and used them extensively.
Updated on June 04, 2022Comments
-
hatellla almost 2 years
Is it possible to have following kind of Step Function graph, i.e. from 2 parallel state output, one combined state:
If yes, what would json for this looks like? If not, why?
-
M. Gleria almost 4 yearsThis works for me. If you're using YAML to describe the state machine on Serverless framework, make sure you don't use '-' for each parameter, since this will make an individual object for each.
-
subhajit biswas over 3 yearsIf suppoe one branch job fails will it wait for other job as well or will the branch fail at once
-
Austin Han Wang over 3 yearsBy default, all branches will fail immediately if one branch job fails.
-
techytushar over 2 yearsIs it guaranteed that the output from the
numbers
step will come at 0 index only, the outputs in the resulting array can be shuffled as well right? -
yakob abada over 2 yearsagree with @techytushar is there any documentation that guaranteed that?
-
maulik13 over 2 years@techytushar yes it does maintain the order. See states-language.net/spec.html#parallel-state
-
Shrinath about 2 yearsTL;DR (from above documentation) -
The elements of the output array correspond to the branches in the same order that they appear in the "Branches" array