简体   繁体   中英

AWS Step-Function: pass a specific value from one AWS lambda to another in step function parallel state

I have the below state machine. The requirement is to have a lambda to query DB and get all the ids. Next I have a parallel state call that calls more than five lambdas at once. Instead of passing all the ids fetched to all the lambdas, I need to pass the respective ids to each lambda.

In the below state language, first call is DB_CALL, lets say it returns {id1, id2, id3, id4, id5, id6}, I want to pass only id1 to First_Lambda and id2 to Second_Lambda etc...

The entire id object should get passed to all lambdas. Please suggest a way to achieve this.

{
    "Comment": "Concurrent Lambda calls",
    "StartAt": "StarterLambda",
    "States": {
        "StarterLambda": {
            "Type": "Task",
            "Resource": "arn:aws:lambda:us-east-1:123456789012:function:DB_CALL",
            "Next": "ParallelCall"
        },
        "State": {
            "ParallelCall": {
                "Type": "Parallel",
                "End": true,
                "Branches": [
                    {
                        "StartAt": "First",
                        "States": {
                            "First": {
                                "Type": "Task",
                                "Resource": "arn:aws:lambda:us-east-1:123456789012:function:First_Lambda",
                                "TimeoutSeconds": 120,
                                "End": true
                            }
                        }
                    },
                    {
                        "StartAt": "Second",
                        "States": {
                            "Second": {
                                "Type": "Task",
                                "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Second_Lambda",
                                "Retry": [ {
                                    "ErrorEquals": ["States.TaskFailed"],
                                    "IntervalSeconds": 1,
                                    "MaxAttempts": 2,
                                    "BackoffRate": 2.0
                                 } ],
                                "End": true
                            }
                        }
                    },
                    {
                        "StartAt": "Third",
                        "States": {
                            "Third": {
                                "Type": "Task",
                                "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Third_Lambda",
                                "Catch": [ {
                                    "ErrorEquals": ["States.TaskFailed"],
                                    "Next": "CatchHandler"
                                 } ],
                                "End": true
                            },
                            "CatchHandler": {
                                "Type": "Pass",
                                "Resource": "arn:aws:lambda:us-east-1:123456789012:function:CATCH_HANDLER",
                                "End": true
                             }
                        }
                    },
                    {
                        "StartAt": "Fourth",
                        "States": {
                            "Fourth": {
                                "Type": "Task",
                                "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Fourth_Lambda",
                                "TimeoutSeconds": 120,
                                "End": true
                            }
                        }
                    },
                    {
                        "StartAt": "Fifth",
                        "States": {
                            "Fifth": {
                                "Type": "Task",
                                "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Fifth_Lambda",
                                "TimeoutSeconds": 120,
                                "End": true
                            }
                        }
                    },
                    {
                        "StartAt": "Sixth",
                        "States": {
                            "Sixth": {
                                "Type": "Task",
                                "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Sixth_Lambda",
                                "TimeoutSeconds": 120,
                                "End": true
                            }
                        }
                     }
                   
                   }
                    
                ]
            }
        }
    }
}

You can use Step Function parameter option. This would allow you to send specific value or json to next lambda.

"Parameters": { "toprocess.$": "$.MetaData.CorrelationId" },

So input to this lambda would be smaller dto than compared to you first lambda. So while returning value from this lambda avoid assigning it back to Step function result.

 "OutputPath": "$",     
 "ResultPath": "$.PartialResutl",

What you are looking for is the Map State . With this state, you pass in the iterator, in your case the path to the ids. The map state will run once for each item in the list. Within the map state, you have a full state machine, so you can call a Lambda or any other state. It has controls to limit how many are running at once if that is needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM