lambda调用函数
In this article, I am going to explain how to create an AWS Lambda function and then call this function from another Lambda function within the same region. This is a useful scenario in which we may need to execute a second lambda function based on the outcome of some previous logic. Another scenario may be to execute a second lambda function several times by using different parameters.
在本文中,我将解释如何创建一个AWS Lambda函数,然后从同一区域内的另一个Lambda函数调用此函数。 这是一个有用的场景,其中我们可能需要根据某些先前逻辑的结果执行第二个lambda函数。 另一种情况可能是使用不同的参数多次执行第二个lambda函数。
For the sake of this article, we will consider a typical retailer application, in which we can purchase different products from a retailer site using a lambda function.
为了本文的目的,我们将考虑一个典型的零售商应用程序,在该应用程序中,我们可以使用lambda函数从零售商站点购买不同的产品。
If you consider the above architecture diagram, you can see that we have an AWS lambda function – the ParentFunction, which assumes a specific role from the IAM (Invoke Other Lambda Function) and then calls another lambda function – the ChildFunction with a payload. Once the execution of the ChildFunction is completed, it returns a response, which is then passed on to the ParentFunction. The ParentFunction receives the response and handles the job accordingly.
如果考虑以上架构图,您会看到我们有一个AWS lambda函数– ParentFunction ,它从IAM( 调用其他Lambda函数 )承担特定角色,然后调用另一个lambda函数–带有有效负载的ChildFunction 。 一旦ChildFunction的执行完成,它将返回一个响应,然后将其传递给ParentFunction 。 ParentFunction接收响应并相应地处理作业。
As in this example, let us assume that the ParentFunction is going to call the ChildFunction with a payload of ProductName, Quantity, and the UnitPrice of that product. The ChildFunction, in turn, will process this payload, calculate the total sales amount, generate a transaction reference ID, and return this information to the ParentFunction.
在此示例中,让我们假设ParentFunction将使用该产品的ProductName,Quantity和UnitPrice的有效负载来调用ChildFunction 。 反过来, ChildFunction将处理此有效负载,计算总销售额,生成交易参考ID,并将此信息返回给ParentFunction 。
创建第一个AWS Lambda函数– ChildFunction (Creating the first AWS Lambda Function – ChildFunction)
Let us first go ahead and create the ChildFunction, which will process the input payload and return the results to the ParentFunction.
让我们首先继续创建ChildFunction,该函数将处理输入的有效负载并将结果返回给ParentFunction。
Head over to https://console.aws.amazon.com/ and login with your credentials. Once you are inside the console, start searching for “Lambda” and click on the first result that appears from the drop-down.
转到https://console.aws.amazon.com/并使用您的凭据登录。 进入控制台后,开始搜索“ Lambda ”,然后单击下拉菜单中显示的第一个结果。
This will take you to the Lambda Function homepage, where you can create a new lambda function by hitting the “Create Function” button.
这将带您到Lambda函数主页,您可以在其中单击“ 创建函数 ”按钮来创建新的lambda函数。
Let the name of this function be – “C