I have written lambda function that retrieve s3 Url whenever any new object created on s3 bucket. after retrieve s3Url this lambda make request to my server over REST Call.
I observed cloud watcher. It failed to send request to my Server I don't want to use external packages in my lambda & i want to make it lightweight that's why i used nodeJ's https.
Here is my Lambda code
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();
};
And here is my cloud watcher Error
{ "errorType": "Error", "errorMessage": "connect ETIMEDOUT 34.255.225.41:443", "code": "ETIMEDOUT", "errno": "ETIMEDOUT", "syscall": "connect", "address": "34.255.225.41", "port": 443, "stack": [ "Error: connect ETIMEDOUT 34.255.225.41:443", " at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14)" ] }