我正在尝试使用 lambda 函数和 node.js 启动 AWS 粘合作业。我可以很好地测试 lambda 函数,但在脚本运行完毕后似乎没有任何反应。我添加了一些 console.log 行,但在调用 SDK 方法来启动 AWS 粘合作业期间,没有任何 console.log 行记录任何内容(我正在检查 lambda 代码配置页面和 CloudWatch 上的输出) 。我在这里错过了什么吗?我使用浏览器内的“测试”按钮测试了以下内容。
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
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;
};
我从控制台得到以下信息:
Response:
{
"statusCode": 200,
"body": "\"Hello from Lambda!\""
}
Request ID:
"e205ec08-dce1-4710-b944-f490544b1486"
Function Logs:
START RequestId: e205ec08-dce1-4710-b944-f490544b1486 Version: $LATEST
2019-05-03T17:17:55.427Z e205ec08-dce1-4710-b944-f490544b1486 Hello!
2019-05-03T17:17:55.525Z e205ec08-dce1-4710-b944-f490544b1486 Done
END RequestId: e205ec08-dce1-4710-b944-f490544b1486
REPORT RequestId: e205ec08-dce1-4710-b944-f490544b1486 Duration: 324.11 ms
Billed Duration: 400 ms Memory Size: 128 MB Max Memory Used: 68 MB
在胶水作业的回调返回之前,您的函数正在返回并关闭。您可以将 return 移动到回调内部,以使回调返回后函数完成
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;
}
});