-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (40 loc) · 1.58 KB
/
index.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
// Load the AWS SDK for Node.js
const AWS = require('aws-sdk');
// To Decompress the Gzip Log
const zlib = require('zlib');
// Set region
AWS.config.update({region: 'us-east-1'});
exports.handler = async (event, context) => {
if (event.awslogs && event.awslogs.data) {
const payload = Buffer.from(event.awslogs.data, 'base64');
const uncompressedLog = JSON.parse(zlib.unzipSync(payload).toString());
let functionName = uncompressedLog.logGroup.split('/')[3];
let errorMsg = uncompressedLog.logEvents[0].message;
let logStream = uncompressedLog.logStream;
let subscriptionFilters = uncompressedLog.subscriptionFilters;
let emailBody = `
One of the Lambda Functions suffered an Error and needs your immediate attention for smooth operation.
Pertinent Details are below:
###########################
Function Name: ${functionName}
Log Stream: ${logStream}
Subscription Filter invoked: ${subscriptionFilters}
Error Message: ${errorMsg}
############################
`
// Create publish parameters
var params = {
Message: emailBody, /* required */
TargetArn: process.env.snsARN, /* required */
Subject: `Attention!! Execution Error for Lambda function - ${functionName}` /* optional */
};
// Create SNS service object
var sns = new AWS.SNS({apiVersion: '2010-03-31'});
try {
let response = await sns.publish(params).promise();
console.log('Success Sending Email. ', response);
} catch(err) {
console.log('Error Encountered.',err)
}
}
};