-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport_count_lambda.js
More file actions
52 lines (36 loc) · 1.45 KB
/
report_count_lambda.js
File metadata and controls
52 lines (36 loc) · 1.45 KB
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
const {
getSsmParameter,
getCustomersCount,
AWS
} = require('./common');
const INSERT_EVENT_NAME = "INSERT";
exports.handler = async function handler(event, context) {
console.log('report_cusotmers_handler:', event);
console.info(`filtering records to eventName=${INSERT_EVENT_NAME}`);
const recordsInserted = event.Records.filter(r => r.eventName === INSERT_EVENT_NAME);
if (recordsInserted.length === 0) {
console.info('no new records.')
return { message: 'no new records... do not notify.', emailSent: false };
}
console.info(`found ${recordsInserted.length} new record(s)`);
const customersCount = await getCustomersCount();
console.info(`total customers count: ${customersCount}`);
const message = `
Records Added: ${recordsInserted.length}
Number of records: ${customersCount}`;
await publishReport(message);
return { message: 'sent notification with customers count.', emailSent: true };
}
async function publishReport(message) {
console.log('publishing message: ', message);
const topicArn = process.env.SNS_TOPIC_ARN
const params = {
Message: message,
TargetArn: topicArn,
Subject: '[devops-academy] Customers Report'
}
const snsClient = new AWS.SNS();
const data = await snsClient.publish(params).promise();
console.log(`Message sent to the topic ${topicArn}`);
console.log(`MessageID ${data.MessageId}`);
}