简体   繁体   English

如何从另一个 Node.js Lambda 函数调用 AWS Python Lambda 函数

[英]How to call an AWS Python Lambda function from another Node.js Lambda function

I'm new to JavaScript and AWS.我是 JavaScript 和 AWS 的新手。 And I'm struggling with connecting a Python AWS Lambda function to a Node.js AWS Lambda function.我正在努力将 Python AWS Lambda 函数连接到 Node.js AWS Lambda 函数。

I want to invoke a separate Lambda function (Let's say child lambda) from a middle line of the main Lambda function (Let's say parent lambda) and use the returned value from the child lambda in the parent lambda.我想从主 Lambda 函数(假设为父 lambda)的中间行调用一个单独的 Lambda 函数(假设为子 lambda),并在父 lambda 中使用子 lambda 的返回值。

My child lambda is implemented in Python 3.6 and parent lambda is implemented in Node.js 12.x.我的子 lambda 是在 Python 3.6 中实现的,父 lambda 是在 Node.js 12.x 中实现的。 I want to stop executing the parent lambda until the child lambda returns it's value.我想停止执行父 lambda,直到子 lambda 返回它的值。

But it seems like the rest of the code lines in the parent lambda execute before finishing the execution of the child lambda.但似乎父 lambda 中的其余代码行在完成子 lambda 的执行之前执行。

I set up the required policy for parent lambda using the below link.我使用以下链接为父 lambda 设置了所需的策略。
https://www.sqlshack.com/calling-an-aws-lambda-function-from-another-lambda-function/ https://www.sqlshack.com/calling-an-aws-lambda-function-from-another-lambda-function/

I have an array assigned to variable 'img'.我有一个分配给变量“img”的数组。 If a condition satisfies I need to invoke the child lambda by passing the Payload as 'img' array to do some extra calculations on that array and return it back to the parent lambda.如果满足条件,我需要通过将有效负载作为 'img' 数组传递来调用子 lambda,对该数组进行一些额外的计算并将其返回给父 lambda。 And finally replace the 'img' variable with the returned array.最后用返回的数组替换 'img' 变量。

Below is the code block I used in the parent lambda to invoke the child lambda.下面是我在父 lambda 中用来调用子 lambda 的代码块。 Please note that "returnArr" is the returned array from the child lambda.请注意,“returnArr”是从子 lambda 返回的数组。

const AWS = require('aws-sdk');
AWS.config.region = 'ap-southeast-2';
var lambda = new AWS.Lambda();

exports.handler = async (event, ctx, callback) => {
    //////////code lines for other operations////////////

    let img = [[[2,3,5],[6,7,8]],[[12,13,15],[16,17,18]]];
    let flag=0;
    if(condition){
        console.log("BEGIN");
        var params = {
                FunctionName: 'childFunction', // child lambda function written in Pyton 3.6
                InvocationType: 'RequestResponse',
                Payload: JSON.stringify({ "sendImg" : img})
            };
            lambda.invoke(params, function(err, data) {
                console.log("SENT");
                if (err) {
                console.log(err);
                } else {
                //console.log('Lambda_B said '+ data.Payload);
                let body1;
                flag = 1;
                if(typeof(data.Payload) == 'object') {
                    body1 = data.Payload;
                } else {
                    body1 = JSON.parse(data.Payload);
                }
                img = body1["returnArray"];
                console.log("Modified img array inside: ",img);
                }
            })
            console.log("DONE");
    }

    console.log("flag : "+flag);
    console.log("Modified img array outside: "+img);
    ////////Rest of the code///////////////////////
};

I get the output as follows:我得到的输出如下:

BEGIN开始
DONE完毕
flag : 0标志:0
Modified img array outside: [[[2,3,5],[6,7,8]],[[12,13,15],[16,17,18]]]外面修改img数组:[[[2,3,5],[6,7,8]],[[12,13,15],[16,17,18]]]
SENT发送
Modified img array inside: [[[23,31,54],[63,71,86]],[[12,133,115],[162,117,18]]]修改里面的img数组:[[[23,31,54],[63,71,86]],[[12,133,115],[162,117,18]]]


According to the output you can see that the 2 code lines outside the if condition have executed before the child lambda return the modified array. 根据输出,您可以看到 if 条件之外的 2 行代码在子 lambda 返回修改后的数组之前已执行。
'img' array outside if condition is not modified but 'img' array inside the if condition is modified. if 条件外的 'img' 数组未修改,但 if 条件内的 'img' 数组已修改。

I want to stop executing the parent lambda until the child lambda returns it's array.我想停止执行父 lambda,直到子 lambda 返回它的数组。

I think this is happening since I don't have much NodeJS knowledge.我认为这是因为我没有太多 NodeJS 知识。
Could someone kindly tell me where I went wrong?有人能告诉我我哪里出错了吗?

the parent lambda execute before finishing the execution of the child lambda父 lambda 在完成子 lambda 的执行之前执行

You are using async handler , so your function finishes before it has a chance to invoke the other function.您正在使用async handler ,因此您的函数在它有机会调用另一个函数之前完成。

To overcome this you can wrap your code in the handler in new Promise as shown in the AWS docs .为了克服这个问题,您可以将您的代码包装在new Promise的处理程序中,如AWS 文档中所示。

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

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