首页 > 解决方案 > 无服务器 Lambda 调用 Lambda。没有错误,但响应为空

问题描述

现在,如果我们想创建一个 lambda 函数来调用另一个函数,我们可以使用 AWS StepFunctions。但是现在我需要支持在 StepFunctions 时间之前编写的生产代码。出于这个原因,我需要了解它是如何工作的。我试图创建一个非常简单的 lambda 调用另一个 lambda 函数 trough AWS-SDk

我有以下 serverless.yml

service: lambdaCallLambda

provider:
  name: aws
  runtime: nodejs6.10

functions:
  hello:
    handler: handler.hello
  funcOne:
    handler: handler.funcOne  
  funcTwo:
    handler: handler.funcTwo

#Must install aws-sdk.  #npm install --save aws-sdk

这是 handler.js:

'use strict';


//https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html
var Lambda = require('aws-sdk/clients/lambda');


module.exports.hello = (event, context, callback) => {
  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: 'hello',
      input: event,
    }),
  };

  callback(null, response);
};


module.exports.funcOne = (event, context, callback) => { 
  var text='';
  var i = 0;
  for (i = 0; i < 5; i++) {
    text += "The number is " + i + "\n";
  }
  console.log(text);


  //https://docs.aws.amazon.com/general/latest/gr/rande.html
  const lambda = new Lambda({
    region: 'us-east-1'
  });
  console.log('control 3');



  /*

    https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#constructor-property
    To invoke a Lambda function
    This operation invokes a Lambda function

    https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html
    Payload - JSON that you want to provide to your Lambda function as input.

  */
  var params = {
    ClientContext: "lambdaCallLambda", 
    FunctionName: "lambdaCallLambda-dev-funcOne", 
    InvocationType: "Event", 
    LogType: "Tail", 
    Payload: '{"jsonKey2":123}', 
    Qualifier: "1"
  };
  lambda.invoke(params, function(err, data) {
    if (err){
      console.log('control error\n');
      console.log(err, err.stack); // an error occurred
    } 
    else{
      console.log('control OK\n');
      console.log(data);           // successful response
    }     
    /*
    data = {
      FunctionError: "", 
      LogResult: "", 
      Payload: <Binary String>, 
      StatusCode: 123
    }
    */
  });  
};


module.exports.funcTwo = async (event, context) => { 
  return 2;
  //return '{"funcTwo":20000}';
  //console.log("funcTwo = " + event);
};  

部署sls deploy和调用后,funcOne我得到这 2 个输出:

当地的: sls invoke local --function funcOne

Serverless: INVOKING INVOKE
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4

control 3
control OK

{ StatusCode: 202, Payload: '' }

在 AWS 中远程调用: sls invoke --function funcOne

{
    "errorMessage": "Unexpected token (",
    "errorType": "SyntaxError",
    "stackTrace": [
        "                               ^",
        "SyntaxError: Unexpected token (",
        "createScript (vm.js:56:10)",
        "Object.runInThisContext (vm.js:97:10)",
        "Module._compile (module.js:542:28)",
        "Object.Module._extensions..js (module.js:579:10)",
        "Module.load (module.js:487:32)",
        "tryModuleLoad (module.js:446:12)",
        "Function.Module._load (module.js:438:3)",
        "Module.require (module.js:497:17)",
        "require (internal/module.js:20:19)"
    ]
}

  Error --------------------------------------------------

  Invoked function failed

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information -----------------------------
     OS:                     linux
     Node Version:           8.11.3
     Serverless Version:     1.29.2

有人知道帽子在这里发生吗?特别是对于我没有任何错误的第一种情况。这是我从文档中得到的

Parameters:

err (Error) — the error object returned from the request. Set to null if the request is successful.
data (Object) — the de-serialized data returned from the request. Set to null if a request error occurs. The data object has the following properties:
Status — (Integer)
It will be 202 upon success.

更新

在 Eduardo Díaz 建议之后 - 我已将 lambda.invoke 更改为:

lambda.invoke({
    FunctionName: 'lambdaCallLambda-dev-funcOne',
    Payload: JSON.stringify(event, null, 2)
  }, function(error, data) {
    if (error) {
      console.log('control ErrorFoncOne\n');
      context.done('error', error);
    }
    if(data.Payload){
     console.log('control SuccessFoncOne\n'); 
     context.succeed(data)
    }
  });

这就是我从本地和远程得到的:

{
    "errorMessage": "Unexpected token (",
    "errorType": "SyntaxError",
    "stackTrace": [
        "Module.load (module.js:487:32)",
        "tryModuleLoad (module.js:446:12)",
        "Function.Module._load (module.js:438:3)",
        "Module.require (module.js:497:17)",
        "require (internal/module.js:20:19)"
    ]
}

这是一个语法错误。某处有一个“(”。我在这里找到了另一个具有相同错误的开发人员。

笔记:

CloudWatch 中没有错误日志

标签: amazon-web-servicesaws-lambdaserverless-framework

解决方案


尝试在有效负载中发送事件:

lambda.invoke({
  FunctionName: 'name_lambda_function',
  Payload: JSON.stringify(event, null, 2)
}, function(error, data) {
  if (error) {
    context.done('error', error);
  }
  if(data.Payload){
   context.succeed(data.Payload)
  }
});

推荐阅读