gpt4 book ai didi

node.js - Lambda/nodejs http 在没有设置 VPC 的情况下超时

转载 作者:搜寻专家 更新时间:2023-11-01 00:03:49 27 4
gpt4 key购买 nike

我正在尝试通过 AWS lambda 中的 Node js 执行 HTTP PUT,但我总是超时。根据this “除非您添加 NAT,否则具有 VPC 访问权限的 Lambda 函数将无法访问互联网”,但就我而言,我没有使用 VPC。

exports.handler = (event, context) => {
const options = {
host: 'xxx',
path: 'xxx',
port: 443,
method: 'PUT'
};
req = http.request(options, (res) => {
console.log(res);
});
};

最佳答案

问题出在 Lambda node.js 上。
如果你想使用 node.js 版本 8,你应该编写这样的代码 example

exports.handler = async (event, context) => {
const options = {
host: 'xxx',
path: 'xxx',
port: 443,
method: 'PUT'
};
const response = await http.request(options);
console.log(response);
};

如果你不想使用node.js version 8,你应该添加第三个参数callback 并在函数执行后调用它。

exports.handler = (event, context, callback) => {
const options = {
host: 'xxx',
path: 'xxx',
port: 443,
method: 'PUT'
};
req = http.request(options, (res) => {
console.log(res);
callback();
});
};

关于node.js - Lambda/nodejs http 在没有设置 VPC 的情况下超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54145045/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com