在AWS Lambda中发送HTTP请求并未获取到数据的问题,可能是由于以下几个原因引起的:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowLambdaHTTPRequests",
"Effect": "Allow",
"Action": [
"ec2:CreateNetworkInterface",
"ec2:DescribeNetworkInterfaces",
"ec2:DeleteNetworkInterface",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}
请注意,上述策略仅仅是一个示例,具体的权限需求可能根据您的实际情况而有所不同。
const https = require('https');
exports.handler = async (event, context) => {
return new Promise((resolve, reject) => {
const options = {
hostname: 'api.example.com',
path: '/data',
method: 'GET'
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve(data);
});
});
req.on('error', (error) => {
reject(error);
});
req.end();
});
};
上述示例使用Node.js的内置https
模块发送HTTP请求,并在收到响应后将数据解析为字符串并返回。
检查以上几点,并根据您的具体情况进行调整,应该能够解决AWS Lambda中HTTP请求未获取到数据的问题。