为什么这个简单的 Lambda 函数的日志中没有显示输出
Why is there no output shown in the logs for this simple Lambda function
这是一个具有 Node.js 14.x 的 lambda 函数
我看到的唯一日志是这个: console.log(event);
None 出现后的日志。响应也返回为 null。我的代码有什么问题吗?
var AWS = require("aws-sdk");
var lambda = new AWS.Lambda();
exports.handler = async function(event, context, callback) {
var params = {
FunctionName: 'Myfunction',
Qualifier: 'v1'
};
console.log(event);
var x =lambda.getProvisionedConcurrencyConfig(params, function(err, data) {
console.log('entered call');
if (err){
console.log('error occured');
console.log(err, err.stack);
response = {
statusCode: 200,
body: JSON.stringify(err),
};
}
else{
console.log('Success');
response = {
statusCode: 200,
body: JSON.stringify(data),
};
console.log(data);
}
});
return response;
};
您的函数在 getProvisionedConcurrencyConfig
完成之前立即完成。您为函数 async function(event, context, callback)
使用回调样式,但从未调用 callback
函数。
第一个解决方案是保持回调风格,你必须调用回调函数:
var AWS = require("aws-sdk");
var lambda = new AWS.Lambda();
exports.handler = function (event, context, callback) { // dont mix callback with async
var params = {
FunctionName: 'Myfunction',
Qualifier: 'v1'
};
console.log(event);
lambda.getProvisionedConcurrencyConfig(params, function (err, data) {
console.log('entered call');
if (err) {
console.log('error occured');
console.log(err, err.stack);
callback(null, { // the function will finish with success status
statusCode: 200,
body: JSON.stringify(err),
});
}
else {
console.log('Success');
console.log(data);
callback(null, {
statusCode: 200,
body: JSON.stringify(data),
});
}
});
};
第二种方案是函数使用Promise风格(推荐):
var AWS = require("aws-sdk");
var lambda = new AWS.Lambda();
exports.handler = async function (event) { // keep as async
var params = {
FunctionName: 'Myfunction',
Qualifier: 'v1'
};
console.log(event);
try {
console.log('entered call');
// wait until it completed
const data = await lambda.getProvisionedConcurrencyConfig(params).promise(); // convert to a promise
console.log('Success');
console.log(data);
return {
statusCode: 200,
body: JSON.stringify(data),
};
} catch (err) {
return { // the function will finish with success status
statusCode: 200,
body: JSON.stringify(err),
};
}
};
这是一个具有 Node.js 14.x 的 lambda 函数
我看到的唯一日志是这个: console.log(event);
None 出现后的日志。响应也返回为 null。我的代码有什么问题吗?
var AWS = require("aws-sdk");
var lambda = new AWS.Lambda();
exports.handler = async function(event, context, callback) {
var params = {
FunctionName: 'Myfunction',
Qualifier: 'v1'
};
console.log(event);
var x =lambda.getProvisionedConcurrencyConfig(params, function(err, data) {
console.log('entered call');
if (err){
console.log('error occured');
console.log(err, err.stack);
response = {
statusCode: 200,
body: JSON.stringify(err),
};
}
else{
console.log('Success');
response = {
statusCode: 200,
body: JSON.stringify(data),
};
console.log(data);
}
});
return response;
};
您的函数在 getProvisionedConcurrencyConfig
完成之前立即完成。您为函数 async function(event, context, callback)
使用回调样式,但从未调用 callback
函数。
第一个解决方案是保持回调风格,你必须调用回调函数:
var AWS = require("aws-sdk");
var lambda = new AWS.Lambda();
exports.handler = function (event, context, callback) { // dont mix callback with async
var params = {
FunctionName: 'Myfunction',
Qualifier: 'v1'
};
console.log(event);
lambda.getProvisionedConcurrencyConfig(params, function (err, data) {
console.log('entered call');
if (err) {
console.log('error occured');
console.log(err, err.stack);
callback(null, { // the function will finish with success status
statusCode: 200,
body: JSON.stringify(err),
});
}
else {
console.log('Success');
console.log(data);
callback(null, {
statusCode: 200,
body: JSON.stringify(data),
});
}
});
};
第二种方案是函数使用Promise风格(推荐):
var AWS = require("aws-sdk");
var lambda = new AWS.Lambda();
exports.handler = async function (event) { // keep as async
var params = {
FunctionName: 'Myfunction',
Qualifier: 'v1'
};
console.log(event);
try {
console.log('entered call');
// wait until it completed
const data = await lambda.getProvisionedConcurrencyConfig(params).promise(); // convert to a promise
console.log('Success');
console.log(data);
return {
statusCode: 200,
body: JSON.stringify(data),
};
} catch (err) {
return { // the function will finish with success status
statusCode: 200,
body: JSON.stringify(err),
};
}
};