简体   繁体   English

从调用的 AWS 返回数据 Lambda

[英]Return data from invoked AWS Lambda

How can I invoke a lambda function from another lambda and get the response data back into the original function to continue processing that data?如何从另一个 lambda 调用 lambda function 并将响应数据返回到原始 function 以继续处理该数据? I have the main lambda function (A) in Node.js, and the second lambda function (B) that is in python. Here is what I need:我在 Node.js 中有主要的 lambda function (A),在 python 中有第二个 lambda function (B)。这是我需要的:

  1. lambda A (node.js) invokes lambda B and passes arguments to it lambda A (node.js) 调用 lambda B 并将 arguments 传递给它
  2. lambda B (python) processes the arguments and generates a response in JSON lambda B (python) 处理 arguments 并在 JSON 中生成响应
  3. lambda A receives the response and does more processing lambda A收到响应,做更多处理
  4. lambda A generates a JSON return/response lambda A 生成 JSON 返回/响应

Not to burden you with my code, here is a simple example of what I'm trying to do.不要用我的代码给你带来负担,这里是我正在尝试做的一个简单示例。

Lambda A Lambda 一

AWS.config.region = 'us-east-2';
var lambda = new AWS.Lambda();

var dataPayloadCopy = 'BLANK';


exports.handler = function(event, context){
  var params = {
    FunctionName: 'arn:aws:lambda:us-east-2:958569232013:function:lambdaB', 
    InvocationType: 'RequestResponse',
    LogType: 'Tail',
    Payload: '{ "name" : "Adam" }'
  };

  var  lambdaResponse = lambda.invoke(params, function(err, data) {
    if (err) {
      console.log("error");
    } else {
      console.log("Response inside callback function: \n" + JSON.stringify(data.Payload));
      //console.log(data.Payload);
      dataPayloadCopy = data.Payload;
    }
  });
  
  console.log("  ------  ");
  console.log("Global response data copy: " + JSON.stringify(dataPayloadCopy));
  console.log("  ------  ");
  console.log("lambdaResponse data: ");
  console.log(lambdaResponse.response.data);
  console.log("  ------  ");
  
  return {
        'statusCode': 200,
        'body': JSON.stringify({"message":"life is good"}),
        'headers': {
            'Content-Type': 'application/json; charset=utf-8',
            'Access-Control-Allow-Origin': '*'
        },
    }
  
}; 

Lambda B Lambda乙

import json
 
def lambda_handler(event,context):
    
    responseMessage = {"message":"Arguments received: name: " + event['name']}
    return {
        'statusCode': 200,
        'body': json.dumps(responseMessage)
    }

Steps 1 and 2 work with no problem.步骤 1 和 2 没有问题。 I can't accomplish step 3. I see the lambda B response in the data.Payload in the lambda.invoke() callback function and can print it out there, but how can I get that value into the main lambda A function so I can work with it there?我无法完成第 3 步。我在 lambda.invoke() 回调 function 中的 data.Payload 中看到了 lambda B 响应,并且可以将其打印出来,但是我如何才能将该值放入主 lambda A function 中,所以我可以在那里工作吗? I tried to copy data.Payload into a global variable dataPayloadCopy, and also assign lambda.invoke() value to lambdaResponse variable (in code above).我尝试将 data.Payload 复制到全局变量 dataPayloadCopy 中,并将 lambda.invoke() 值分配给 lambdaResponse 变量(在上面的代码中)。 Neither way worked.两种方法都行不通。 Looks like the lambda B executes when lambda A finishes.看起来 lambda B 在 lambda A 完成时执行。

Here is the execution result:下面是执行结果:

INFO      ------  
INFO    Global response data copy: "BLANK"
INFO      ------  
INFO    lambdaResponse data: 
INFO    null
INFO      ------  
INFO    Response inside callback function: 
"{\"statusCode\": 200, \"body\": \"{\\\"message\\\": \\\"Arguments received: name: Adam\\\"}\"}"

Also, I don't see the lambda A return value in the test results.另外,我在测试结果中没有看到 lambda A 返回值。 If I change the handler function in lambda A to be asynchronous exports.handler = async (event) => { and run a test, lambda A return value shows, but there is no printout from the callback function appears as if lambda B was not invoked and/or the return was not received.如果我将 lambda A 中的处理程序 function 更改为异步 exports.handler exports.handler = async (event) => {并运行测试, lambda A 返回值显示,但回调 function 没有打印输出,就好像 lambda B 不是调用和/或未收到返回。

I did not have any problem with a similar task if both lambdas are in python. My lambda A code is in Node.js though.如果两个 lambda 都在 python 中,我对类似任务没有任何问题。不过我的 lambda A 代码在 Node.js 中。

The Lambda invoke function is asynchronous and uses callbacks. Lambda 调用 function 是异步的并使用回调。 Hence, while your invoke is calling the Python Lamdda function, the rest of your code outside of the callback is continuing to run.因此,当您的调用调用 Python Lamdda function 时,回调之外的代码 rest 将继续运行。

The best approach for you would be to change the next steps of your code inside of the callback function like below,对您来说最好的方法是在回调 function 中更改代码的后续步骤,如下所示,

AWS.config.region = "us-east-2";
var lambda = new AWS.Lambda();

var dataPayloadCopy = "BLANK";

exports.handler = function (event, context) {
  var params = {
    FunctionName: "arn:aws:lambda:us-east-2:958569232013:function:lambdaB",
    InvocationType: "RequestResponse",
    LogType: "Tail",
    Payload: '{ "name" : "Adam" }',
  };

  var lambdaResponse = lambda.invoke(params, function (err, data) {
    if (err) {
      console.log("error");
    } else {
      console.log(
        "Response inside callback function: \n" + JSON.stringify(data.Payload)
      );
      //console.log(data.Payload);
      dataPayloadCopy = data.Payload;
    }
  });
};

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

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