forked from jamesjessian/queens-completion-lambda-kinesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambdaHandler.js
58 lines (50 loc) · 1.52 KB
/
lambdaHandler.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
const AWS = require('aws-sdk'); // eslint-disable-line import/no-extraneous-dependencies
const config = require('./config')
const processSet = require('./process')
const convert = require('./convertKinesisRecord')
const kinesis = new AWS.Kinesis()
const dynamoDb = new AWS.DynamoDB.DocumentClient()
/**
* This is our AWS Lambda event handler
*/
function process(event, context, callback) {
event.Records.forEach(record => {
const set = convert(record)
// Pass data and callback functions to processor
processSet(set, addNewSet, publishResult)
})
}
module.exports.process = process
// ========================================================================
// Private functions
// ========================================================================
function publishResult(value) {
const params = {
TableName: config.RESULTS_TABLE,
Item: {
key: value.toString(16),
size: config.BOARD_SIZE,
timestamp: new Date().toString()
},
}
// Write the record to the database
dynamoDb.put(params, err => {
if(err) console.error(err, err.stack)
})
}
function addNewSet(set) { // {key, level, connections (as BNs)}
const key = set.key.toString(16)
const params = {
Data: JSON.stringify({
level: set.level,
key: key,
connections: set.connections.toString(16),
}),
PartitionKey: key,
StreamName: config.KINESIS_WORK_STREAM,
}
// Write record to stream
kinesis.putRecord(params, err => {
if(err) console.error(err, err.stack)
})
}