Skip to content

Commit

Permalink
aws-samples#7 - context.succeed would be called multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh-amzn committed Sep 28, 2016
1 parent e8bccbd commit f99c4ad
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/kinesis_lambda_es.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/* == Imports == */
var AWS = require('aws-sdk');
var path = require('path');
var when = require('when');

/* == Globals == */
var esDomain = {
Expand All @@ -39,17 +40,21 @@ var creds = new AWS.EnvironmentCredentials('AWS');
/* Lambda "main": Execution begins here */
exports.handler = function(event, context) {
console.log(JSON.stringify(event, null, ' '));
var promises = [];
event.Records.forEach(function(record) {
var jsonDoc = new Buffer(record.kinesis.data, 'base64');
postToES(jsonDoc.toString(), context);
postToES(jsonDoc.toString(), context, promises);
});
when.all(promises).then(function(res) {
context.succeed('Lambda Event Processed');
})
}


/*
* Post the given document to Elasticsearch
*/
function postToES(doc, context) {
function postToES(doc, context, promises) {
var req = new AWS.HttpRequest(endpoint);

req.method = 'POST';
Expand All @@ -63,17 +68,19 @@ function postToES(doc, context) {
signer.addAuthorization(creds, new Date());

var send = new AWS.NodeHttpClient();
var deferred = when.defer();
send.handleRequest(req, null, function(httpResp) {
var respBody = '';
httpResp.on('data', function (chunk) {
respBody += chunk;
});
httpResp.on('end', function (chunk) {
console.log('Response: ' + respBody);
context.succeed('Lambda added document ' + doc);
promises.push(deferred.reject);
});
}, function(err) {
console.log('Error: ' + err);
context.fail('Lambda failed with error ' + err);
promises.push(deferred.reject);
});
}

0 comments on commit f99c4ad

Please sign in to comment.