我编写了lambda函数,每当在s3存储桶上创建任何新对象时,该函数都会检索s3 Url。在检索s3Url之后,该lambda发出通过REST调用向我的服务器发出的请求。
我观察了云观察者。无法将请求发送到我的服务器我不想在我的lambda中使用外部软件包,我想使其轻巧,这就是为什么我使用nodeJ的原因https。
这是我的Lambda代码
exports.handler = (event,context,callback) => {
// Extract S3 Url and id From S3 object present in event
const https = require('https');
let {s3 , awsRegion} = event["Records"][0];
let {object : {key}, bucket : {name}} = s3;
let s3URL = `https://${name}.s3.${awsRegion}.amazonaws.com/${key}`;
console.log("sURL",s3URL);
let _id = key.split('/')[0];
console.log("id",_id);
//Making http request to my server
let body='';
// the post options
let optionsPost = {
host: 'xyz.abc.com',
path: '/api/v1/apipath',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'XYZ',
'id' : _id
}
};
const keepAliveAgent = new https.Agent({ keepAlive: true });
optionsPost.agent = keepAliveAgent;
let reqPost = https.request(optionsPost, function(res) {
console.log("statusCode: ", res.statusCode);
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
console.log("Result", body.toString());
context.succeed("Sucess")
});
res.on('error', function () {
console.log("Result Error", body.toString());
context.done(null, 'FAILURE');
callback(new Error('failure')) // to return error
});
});
reqPost.write(JSON.stringify({ s3Url: s3URL,userName: 'SYSTEM' }));
reqPost.end();
};
这是我的云监视程序错误
{“ errorType”:“错误”,“ errorMessage”:“连接ETIMEDOUT 34.255.225.41:443”,“ code”:“ ETIMEDOUT”,“ errno”:“ ETIMEDOUT”,“ syscall”:“连接”,“地址”:“ 34.255.225.41”,“端口”:443,“堆栈”:[“错误:在TCPConnectWrap.afterConnect上连接ETIMEDOUT 34.255.225.41:443”,“ [在完成时](net.js:1107:14) “]}
[如果您尝试通过AWS开发工具包或直接通过Lambda进行任何HTTP调用来访问S3(或其他AWS资源),则两种情况之一必须为true:
您的代码正在使用面向公众的域名从S3检索数据。 VPC本身不为面向公众的地址提供路由。如果您尝试在VPC后面运行Lambda,并且未设置NAT网关来理解路由位置,则当您的代码尝试调用面向公众的AWS资源(在本例中为S3)时,函数将出现ETIMEDOUT
错误通过http请求。
出于相同原因,在Lambda中使用AWS开发工具包时也会发生这种情况。
您应该在VPC上设置NAT网关。有关如何执行此操作的更多信息,请参见AWS' Documentation