有没有办法从 aws lambda 函数(节点 js)调用 Rest API
Is there a way to call the Rest API from the aws lamda functions ( node js)
我想从 lambda 函数调用外部 API 以实现 LEX 机器人意图,但无法与外部 API 通信,这些 API 托管在其他地方。相同的 JS 代码在我的本地系统中运行,但无法通过 lambda 函数进行通信。所以这不是服务的问题,更像是 AWS 云网络或相关问题。看了一下cloud watch日志,没有报错
我没有使用 VPC,我的功能在 VPC 之外。任何帮助将不胜感激
exports.handler = async (event) => {
console.log ("executing222222") ;
var https = require('https');
var options = {
'method': 'POST',
'hostname': 'ccc.com',
'path': '/xxx',
'headers': {
'Authorization': 'bearer6ad1a3ae-2a1d-48e0-bf68-8669c5b9af62'
}
};
console.log ("test");
var req = https.request(options, function (res) {
console.log ("test1111");
res.setEncoding('utf8');
var returnData = "";
res.on('data', function (chunk) {
returnData += chunk;
});
console.log ("test11");
res.on("end", function () {
var body = JSON.parse(returnData) ;
console.log(body.toString());
});
res.on("error", function (error) {
console.error(error);
});
});
req.end();
};
此代码有助于解决异步问题。
const http = require('http');
exports.handler = async (event, context) => {
return new Promise((resolve, reject) => {
const options = {
host: 'ec2-18-191-89-162.us-east-2.compute.amazonaws.com',
path: '/api/repos/r1639420d605/index?delta=true&clear=false',
port: 8000,
method: 'PUT'
};
const req = http.request(options, (res) => {
resolve('Success');
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.write('');
req.end();
});
};`enter code here`
我想从 lambda 函数调用外部 API 以实现 LEX 机器人意图,但无法与外部 API 通信,这些 API 托管在其他地方。相同的 JS 代码在我的本地系统中运行,但无法通过 lambda 函数进行通信。所以这不是服务的问题,更像是 AWS 云网络或相关问题。看了一下cloud watch日志,没有报错
我没有使用 VPC,我的功能在 VPC 之外。任何帮助将不胜感激
exports.handler = async (event) => {
console.log ("executing222222") ;
var https = require('https');
var options = {
'method': 'POST',
'hostname': 'ccc.com',
'path': '/xxx',
'headers': {
'Authorization': 'bearer6ad1a3ae-2a1d-48e0-bf68-8669c5b9af62'
}
};
console.log ("test");
var req = https.request(options, function (res) {
console.log ("test1111");
res.setEncoding('utf8');
var returnData = "";
res.on('data', function (chunk) {
returnData += chunk;
});
console.log ("test11");
res.on("end", function () {
var body = JSON.parse(returnData) ;
console.log(body.toString());
});
res.on("error", function (error) {
console.error(error);
});
});
req.end();
};
此代码有助于解决异步问题。
const http = require('http');
exports.handler = async (event, context) => {
return new Promise((resolve, reject) => {
const options = {
host: 'ec2-18-191-89-162.us-east-2.compute.amazonaws.com',
path: '/api/repos/r1639420d605/index?delta=true&clear=false',
port: 8000,
method: 'PUT'
};
const req = http.request(options, (res) => {
resolve('Success');
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.write('');
req.end();
});
};`enter code here`