Created
February 1, 2016 12:43
-
-
Save cmilfont/fe9ef35742aecf306780 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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