Skip to content

Commit

Permalink
readme file Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
alandavid committed Apr 29, 2022
1 parent a379875 commit a0bde22
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 70 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ override.tf.json

# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*
lambda/node_modules
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Terraform module to provision an CloudTrail with a custom notification.
- [SNS](https://docs.aws.amazon.com/sns/latest/dg/sns-create-topic.html) subscription topic responsible to send the custom notification.

## Triggers
* The Lambda function is triggered on a SNS event.
* The Lambda function is triggered on a EventBridge event.
* You need to provide the SNS topic.


Expand Down
79 changes: 10 additions & 69 deletions lambda/index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
var AWS = require('aws-sdk')
var url = require('url')
var https = require('https')
var config = require('./config')
var _ = require('lodash')
var cwl = new AWS.CloudWatchLogs();
var cw = new AWS.CloudWatch();
var sns = new AWS.SNS({ apiVersion: '2010-03-31' });

exports.handler = function (event, context) {
var alarmName = event.detail.alarmName;
var time = event.time;
console.info(event)
processEvent(event, context);
};

Expand All @@ -32,29 +25,22 @@ var processEvent = function (event, context) {

function getWatchlogData(alarmName, time, fn) {
getAlarm(alarmName, function (err, data) {
console.log(data)
if (err) { console.log(err, err.stack) } // an error occurred
else {
const alarms = data.MetricAlarms[0]
getMetricsFilter(data.MetricAlarms[0].MetricName, data.MetricAlarms[0].Namespace, function (err, data) {
console.log("getMetricsFilter")
console.log(data)
if (err) console.log('Error is:', err);
else {
console.log("getFilterLogEvents")
// if (data.metricFilters[0].logGroupName == process.env.LOG_GROUP) {
getFilterLogEvents(data.metricFilters[0].logGroupName, data.metricFilters[0].filterPattern, time, function (err, data) {
if (err) console.log('Error is:', err);
else {
console.log(data)
fn(err, data, alarms)
}

})
// } else {
// console.error(`Event:${data.eventName} not found`)
// }

if (data.metricFilters[0].logGroupName == process.env.LOG_GROUP) {
getFilterLogEvents(data.metricFilters[0].logGroupName, data.metricFilters[0].filterPattern, time, function (err, data) {
if (err) console.log('Error is:', err);
else {
fn(err, data, alarms)
}
})
} else {
console.info(`Event:${data.eventName} not found`)
}
}
})
}
Expand Down Expand Up @@ -82,16 +68,12 @@ function getFilterLogEvents(logGroupName, filterPattern, time, fn) {
var startTime = new Date(time);
var endTime = new Date(time);
startTime.setSeconds(startTime.getSeconds() - process.env.OFFSET);
console.error(startTime)
console.error(endTime)
var parameters = {
'logGroupName': logGroupName,
'filterPattern': filterPattern,
'startTime': startTime.getTime(),
'endTime': endTime.getTime()
};
console.log(parameters)

cwl.filterLogEvents(parameters, fn);
}

Expand All @@ -103,7 +85,6 @@ var sendEmail = function (structureMessage, callback) {
MessageStructure: 'string'
};
console.log("===SENDING EMAIL===");

var email = sns.publish(params).promise().then(function (data) {
console.log(`Message ${params.Message} sent to the topic ${params.TopicArn}`);
console.log("MessageID is " + data.MessageId);
Expand Down Expand Up @@ -146,44 +127,4 @@ function generateEmailContent(clouldTrail, alarm, events) {
customMessage = customMessage + test;

return { message: customMessage, subject: `ALARM: "${alarm.AlarmName}" in ${clouldTrail.awsRegion}` };
}



var handleCatchAll = function (event, context) {
var record = event.Records[0]
var subject = record.Sns.Subject
var timestamp = new Date(record.Sns.Timestamp).getTime() / 1000
var message = JSON.parse(record.Sns.Message)
var color = 'warning'

if (message.NewStateValue === 'ALARM') {
color = 'danger'
} else if (message.NewStateValue === 'OK') {
color = 'good'
}

// Add all of the values from the event message to the Slack message description
var description = ''
for (key in message) {
var renderedMessage = typeof message[key] === 'object' ? JSON.stringify(message[key]) : message[key]

description = description + '\n' + key + ': ' + renderedMessage
}

var slackMessage = {
text: '*' + subject + '*',
attachments: [
{
color: color,
fields: [
{ title: 'Message', value: record.Sns.Subject, short: false },
{ title: 'Description', value: description, short: false }
],
ts: timestamp
}
]
}

return _.merge(slackMessage, baseSlackMessage)
}

0 comments on commit a0bde22

Please sign in to comment.