-
Notifications
You must be signed in to change notification settings - Fork 3
/
scanner.js
40 lines (31 loc) · 1.14 KB
/
scanner.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
'use strict';
const DynamoDB = require('aws-sdk/clients/dynamodb');
const dbOptions = {
region: 'us-east-1'
}
const dynamo = new DynamoDB(dbOptions);
const documentClient = new DynamoDB.DocumentClient({ service: dynamo });
module.exports.handler = async (event) => {
const [sqsEvent] = event.Records;
const params = JSON.parse(sqsEvent.body);
console.log('SCAN STARTING', params);
let counter = 0;
let ExclusiveStartKey;
try {
do {
const { Items, LastEvaluatedKey } = await documentClient.scan(params).promise();
await doSomethingWithData(Items);
counter += Items.length;
ExclusiveStartKey = LastEvaluatedKey || false;
params.ExclusiveStartKey = ExclusiveStartKey;
} while (ExclusiveStartKey);
} catch (error) {
console.error(`[SCANNER ERROR] - scanner segment ${params.Segment} encountered an error on table ${params.TableName}`, error);
return { statusCode: 500 };
}
console.log(`[SCANNER SUCCESS] - scanner segment ${params.Segment} successfully scanned ${counter} items from ${params.TableName}`);
return { statusCode: 200 };
}
async function doSomethingWithData(data) {
return true;
}