Node.js HTTPS调用在AWS Lambda中不起作用
我正在尝试创建一个AWS Lambda函数,该函数将在计划中调用DELETE 我正在使用Node.js。在本地机器上仅从Node.js运行时,请求工作正常 代码如下:Node.js HTTPS调用在AWS Lambda中不起作用,node.js,amazon-web-services,aws-lambda,Node.js,Amazon Web Services,Aws Lambda,我正在尝试创建一个AWS Lambda函数,该函数将在计划中调用DELETE 我正在使用Node.js。在本地机器上仅从Node.js运行时,请求工作正常 代码如下: const https = require('https'); var options = { host: 'MY_HOST', path: '/v1/api/orders', port: 80, method: 'DELETE' }; console.info('Do the DELETE c
const https = require('https');
var options = {
host: 'MY_HOST',
path: '/v1/api/orders',
port: 80,
method: 'DELETE'
};
console.info('Do the DELETE call');
var reqDelete = https.request(options, function(res) {
res.on('data', function(d) {
console.info('DELETE result:\n');
console.log(d.toString('utf8'));
console.info('\nCall completed');
});
});
reqDelete.on('error', function(e) {
console.error(e);
});
reqDelete.end();
我的输出如下:
Do the DELETE call
DELETE result:
{"message":"Cleanup triggered"}
Call completed
exports.handler = async (event) => {
// The exact code from above with the actual host name.
};
正如我所料。但是,当我从AWS Lambda函数内部运行时,得到的结果是null,Lambda函数的日志输出如下
START RequestId: fb2a1969-94e8-4c11-b43e-14ff6a4cc426 Version: $LATEST
2020-06-16T01:42:06.875Z fb2a1969-94e8-4c11-b43e-14ff6a4cc426 INFO Do the DELETE call
END RequestId: fb2a1969-94e8-4c11-b43e-14ff6a4cc426
REPORT RequestId: fb2a1969-94e8-4c11-b43e-14ff6a4cc426 Duration: 483.49 ms Billed Duration: 500 ms
Memory Size: 128 MB Max Memory Used: 67 MB Init Duration: 125.35 ms
注意,它打印出dothedelete调用,这样我就知道它进入了我的代码,但没有打印出任何其他内容
Lambda的主体如下所示:
Do the DELETE call
DELETE result:
{"message":"Cleanup triggered"}
Call completed
exports.handler = async (event) => {
// The exact code from above with the actual host name.
};
当Lambda函数在本地计算机上运行时,为什么不从Lambda函数执行API调用?您正在使用,但HTTP请求未被等待。这会导致函数在https.request调用完成之前完成执行
如果要使用回调而不是承诺,请为函数定义非异步处理程序:
exports.handler = (event) => {
// The exact code from above with the actual host name.
};
在console语句之后添加lambda callback,如果lambda正在获取timedout,则您正在调用的端点不具有lambda的访问权限。这是由于lambda和API VPC配置造成的。另一种想法是,您的代码在本地计算机上运行,因为API允许从您的办公室IP地址发出请求。