使用 Node.JS 调用 AWS 胶水的 lambda 函数不使用 console.log 的原因是啥?

Posted

技术标签:

【中文标题】使用 Node.JS 调用 AWS 胶水的 lambda 函数不使用 console.log 的原因是啥?【英文标题】:Reason why lambda function calling AWS glue using Node.JS does not console.log?使用 Node.JS 调用 AWS 胶水的 lambda 函数不使用 console.log 的原因是什么? 【发布时间】:2019-09-22 06:37:56 【问题描述】:

我正在尝试使用 node.js 使用 lambda 函数启动 AWS 粘合作业。我可以很好地测试 lambda 函数,但脚本运行后似乎没有任何反应。我添加了一些 console.log 行,但是在 SDK 方法调用以启动 AWS 粘合作业期间,没有任何 console.log 行记录任何内容(我正在检查 lambda 代码配置页面和 CloudWatch 上的输出) .我在这里错过了什么吗?我使用浏览器内的“测试”按钮测试了以下内容。

var AWS = require('aws-sdk'); AWS.config.update(region: 'us-east-2');

var 胶水 = 新 AWS.Glue();

exports.handler = 异步(事件)=>

console.log("Hello!")
var params = 
        JobName: 'ETL-store-inventory',
    ;

//Invoke job run
glue.startJobRun(params, function(err, data) 
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
);

console.log("Done")

const response = 
    statusCode: 200,
    body: JSON.stringify('Hello from Lambda!'),
;
return response;

;

我从控制台得到以下信息:

回应: “状态码”:200, "body": "\"来自 Lambda 的你好!\""

请求 ID: “e205ec08-dce1-4710-b944-f490544b1486”

功能日志: 开始请求 ID:e205ec08-dce1-4710-b944-f490544b1486 版本:$LATEST

2019-05-03T17:17:55.427Z e205ec08-dce1-4710-b944-f490544b1486 你好!

2019-05-03T17:17:55.525Z e205ec08-dce1-4710-b944-f490544b1486 完成

END 请求 ID:e205ec08-dce1-4710-b944-f490544b1486

报告请求 ID:e205ec08-dce1-4710-b944-f490544b1486 持续时间:324.11 毫秒

计费持续时间:400 毫秒内存大小:128 MB 使用的最大内存:68 MB

【问题讨论】:

您的函数在胶水作业的回调返回之前返回并关闭。您可以将粘合作业变成一个承诺并等待它完成,或者将返回调用移动到回调中。 【参考方案1】:

您的函数在胶水作业的回调返回之前返回并关闭。您可以在回调中移动返回以在回调返回后完成函数

var AWS = require('aws-sdk'); AWS.config.update(region: 'us-east-2');

var glue = new AWS.Glue();

exports.handler = async (event) => 

console.log("Hello!")
var params = 
        JobName: 'ETL-store-inventory',
    ;

//Invoke job run
return glue.startJobRun(params, function(err, data) 
  if (err) 
    console.log(err, err.stack); // an error occurred
    const response = 
      statusCode: 200,
      body: JSON.stringify('An error occurred!'),
    ;
    return response
   else  
    console.log(data);           // successful response
    console.log("Done")
    const response = 
      statusCode: 200,
      body: JSON.stringify('Hello from Lambda!'),
    ;
    return response;
  
);


【讨论】:

以上是关于使用 Node.JS 调用 AWS 胶水的 lambda 函数不使用 console.log 的原因是啥?的主要内容,如果未能解决你的问题,请参考以下文章