This repository has been archived by the owner on Oct 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 345
/
ZombieWorkshopSearchIndexing.js
72 lines (63 loc) · 2.25 KB
/
ZombieWorkshopSearchIndexing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
var AWS = require('aws-sdk');
var path = require('path');
/* == Globals == */
var esDomain = {
region: 'us-west-2',
endpoint: 'https://ENDPOINT_HERE',
index: 'messages',
doctype: 'message'
};
var endpoint = new AWS.Endpoint(esDomain.endpoint);
var creds = new AWS.EnvironmentCredentials('AWS');
/* Lambda "main": Execution begins here */
exports.handler = function(event, context) {
console.log(JSON.stringify(event, null, ' '));
event.Records.forEach(function(record) {
if (typeof record.dynamodb.NewImage != 'undefined')
{
var doc = {message: {
name: record.dynamodb.NewImage.name.S,
message: record.dynamodb.NewImage.message.S,
channel: record.dynamodb.NewImage.channel.S,
timestamp: record.dynamodb.NewImage.timestamp.N}
}
console.log('document posted to ElasticSearch: ' + JSON.stringify(doc))
postToES(JSON.stringify(doc), context);
}
else
{
console.log('skipping non-inserts');
}
});
}
/*
* Post the given document to Elasticsearch
*/
function postToES(doc, context) {
var req = new AWS.HttpRequest(endpoint);
console.log('create post request');
req.method = 'POST';
req.path = path.join('/', esDomain.index, esDomain.doctype);
req.region = esDomain.region;
req.headers['presigned-expires'] = false;
req.headers['Host'] = endpoint.host;
req.body = doc;
console.log('Creating the Signer for the post request');
var signer = new AWS.Signers.V4(req , 'es'); // es: service code
signer.addAuthorization(creds, new Date());
console.log('Sending Data');
var send = new AWS.NodeHttpClient();
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);
});
}, function(err) {
console.log('Error: ' + err);
context.fail('Lambda failed with error ' + err);
});
}