Skip to content

Instantly share code, notes, and snippets.

@cmilfont
Created February 1, 2016 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmilfont/fe9ef35742aecf306780 to your computer and use it in GitHub Desktop.
Save cmilfont/fe9ef35742aecf306780 to your computer and use it in GitHub Desktop.
exports.handler = function(event, context) {
var path = require('path');
var AWS = require('aws-sdk');
var esDomain = {
endpoint: 'search-zonaextrema-po7opa5ghqiby3jndavrzdyyqe.us-west-2.es.amazonaws.com',
region: 'us-west-2',
index: 'biohacking',
doctype: 'logs'
};
var endpoint = new AWS.Endpoint(esDomain.endpoint);
var creds = new AWS.EnvironmentCredentials('AWS');
var req = new AWS.HttpRequest(endpoint);
req.method = 'POST';
req.path = path.join('/', esDomain.index, esDomain.doctype);
req.region = esDomain.region;
req.body = JSON.stringify(event.log);
req.headers['presigned-expires'] = false;
req.headers['Host'] = endpoint.host;
// Sign the request (Sigv4)
var signer = new AWS.Signers.V4(req, 'es');
signer.addAuthorization(creds, new Date());
// Post document to ES
var send = new AWS.NodeHttpClient();
send.handleRequest(req, null, function(httpResp) {
var body = '';
httpResp.on('data', function (chunk) {
body += chunk;
});
httpResp.on('end', function (chunk) {
numDocsAdded ++;
if (numDocsAdded === totLogLines) {
// Mark lambda success. If not done so, it will be retried.
console.log('All ' + numDocsAdded + ' log records added to ES.');
context.succeed(numDocsAdded);
}
});
context.done(null, body);
}, function(err) {
console.log('Error: ' + err);
console.log(numDocsAdded + 'of ' + totLogLines + ' log records added to ES.');
context.fail(err);
});
//context.done(null, signer);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment