简体   繁体   English

AWS Step-Function:在步骤 function 并行 state 中将特定值从一个 AWS lambda 传递到另一个

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

I have the below state machine.我有下面的 state 机器。 The requirement is to have a lambda to query DB and get all the ids.要求是有一个 lambda 来查询数据库并获取所有 ID。 Next I have a parallel state call that calls more than five lambdas at once.接下来我有一个并行的 state 调用,它一次调用五个以上的 lambda。 Instead of passing all the ids fetched to all the lambdas, I need to pass the respective ids to each lambda.我需要将各自的 id 传递给每个 lambda,而不是将获取的所有 id 传递给所有 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...在下面的 state 语言中,第一个调用是 DB_CALL,假设它返回 {id1, id2, id3, id4, id5, id6},我只想将 id1 传递给 First_Lambda 并将 id2 传递给 Second_Lambda 等...

The entire id object should get passed to all lambdas.整个 id object 应该传递给所有 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.您可以使用 Step Function 参数选项。 This would allow you to send specific value or json to next lambda.这将允许您将特定值或 json 发送到下一个 lambda。

"Parameters": { "toprocess.$": "$.MetaData.CorrelationId" }, “参数”:{“toprocess.$”:“$.MetaData.CorrelationId”},

So input to this lambda would be smaller dto than compared to you first lambda.因此,与第一个 lambda 相比,输入到这个 lambda 的 dto 会更小。 So while returning value from this lambda avoid assigning it back to Step function result.因此,在从此 lambda 返回值时,请避免将其分配回步骤 function 结果。

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

What you are looking for is the Map State .您正在寻找的是Map State With this state, you pass in the iterator, in your case the path to the ids.使用此 state,您可以传入迭代器,在您的情况下是 id 的路径。 The map state will run once for each item in the list. map state 将为列表中的每个项目运行一次。 Within the map state, you have a full state machine, so you can call a Lambda or any other state. 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.如果需要,它可以控制一次运行的数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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