{Status: 202} 从代码运行 lambda 时

Ama*_*day 5 javascript aws-lambda

这是我的 JavaScript 代码:

    var params = {
        FunctionName: "theTable",
        InvokeArgs: JSON.stringify({ "name": "KirklandWA" })
    };
    lambda.invokeAsync(params, function (err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else {
            console.log(data);
        }
    });
Run Code Online (Sandbox Code Playgroud)

这是在 Lambda 中:

exports.handler = async (event, context) => {
    return "theReturnedValue";
};
Run Code Online (Sandbox Code Playgroud)

发生的事情是它没有返回theReturnedValue,而是返回

{状态:202} 状态:202

中的代码Lambda正在被调用,我在 中确认了这一点Cloudwatch

Van*_*anh 12

您调用 withinvokeAsync只会返回文档中所述的状态代码。使用invokewithInvocationType: "RequestResponse"代替

参考: https: //docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#invoke-property

var lambda = new AWS.Lambda({});
var params = {
  FunctionName: "function_name", 
  InvocationType: "RequestResponse"
};
response = lambda.invoke(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});
Run Code Online (Sandbox Code Playgroud)


Tii*_*ane -1

问题是您lambda function没有将任何内容返回给调用者。

您的句柄函数有第三个参数,它是一个回调函数,用于将结果返回给调用者。

callback函数接受两个值,一个错误和结果

callback(Error error, Object result);
Run Code Online (Sandbox Code Playgroud)

如果您提供错误值,lambda 将抛出您提供给用户的错误,如果您不提供错误值但提供结果,则将返回结果

这里都记录得很好

以下是基本示例

callback();     // Indicates success but no information returned to the caller.
callback(null); // Indicates success but no information returned to the caller.
callback(null, "success");  // Indicates success with information returned to the caller.
callback(error);    //  Indicates error with error information returned to the caller.
Run Code Online (Sandbox Code Playgroud)

你的处理函数应该是。

exports.handler = async (event, context,callback) => {
     callback(null, "theReturnedValue");
};
Run Code Online (Sandbox Code Playgroud)