Skip to content

Commit fbe256e

Browse files
committed
Add Readme file for cloudwatchlogs folder, also add a special version for AWS Lambda logs
1 parent b4891ac commit fbe256e

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

cloudwatchlogs/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
SumoLogic Functions for AWS CloudWatch Logs
2+
===========================================
3+
4+
Files
5+
-----
6+
* *cloudwatchlogs.js*: node.js file to collect data from AWS CWL. Can also be used to collect AWS VPC Flowlogs sent via CWL.
7+
* *cloudwatchlogs_lambda.js*: node.js file to collect AWS Lambda logs via CWL. This version extracts and add a "RequestId" field to each log line to make correlations easier.
8+
9+
Usage
10+
-----
11+
1. First create an HTTP source endpoint on the Sumo side. You will need this endpoint for the lambda function later.
12+
2. Goto AWS CloudWatch Logs console, check the Log Group you want to send data to Sumologic. From Actions button, select "Start Streaming to Lambda Service", then "Create a Lambda function"
13+
3. Skip the blueprint
14+
4. Copy the relevant lambda function to the console. **REMEMBER** to replace the value of *hostname* in the function with the relevant value for your SumoLogic account, and of the *path* with HTTP endpoint created in the first step above.
15+
5. Scroll down to the *Lambda function handle and role* section, make sure you set the right values that match the function. For role, you can just use the basic execution role. Click next.
16+
6. Finally click on "Create function" to create the function.
17+
7. (Optional) Test this new function with sample AWS CloudWatch Logs template provided by AWS
18+
19+
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
var https = require('https');
3+
var zlib = require('zlib');
4+
5+
exports.handler = function(event, context) {
6+
///////////////////////////////////////////////////////////////////////////////////////////////////////////
7+
// Remember to change the hostname and path to match your collection API and specific HTTP-source endpoint
8+
// See more at: https://service.sumologic.com/help/Default.htm#Collector_Management_API.htm
9+
///////////////////////////////////////////////////////////////////////////////////////////////////////////
10+
var options = { 'hostname': 'collectors.sumologic.com',
11+
'path': 'https://collectors.sumologic.com/receiver/v1/http/<XXX>',
12+
'method': 'POST'
13+
};
14+
var zippedInput = new Buffer(event.awslogs.data, 'base64');
15+
16+
zlib.gunzip(zippedInput, function(e, buffer) {
17+
if (e) { context.fail(e); }
18+
19+
awslogsData = JSON.parse(buffer.toString('ascii'));
20+
21+
console.log(awslogsData);
22+
23+
if (awslogsData.messageType === "CONTROL_MESSAGE") {
24+
console.log("Control message");
25+
context.succeed("Success");
26+
}
27+
28+
var req = https.request(options, function(res) {
29+
var body = '';
30+
console.log('Status:', res.statusCode);
31+
res.setEncoding('utf8');
32+
res.on('data', function(chunk) { body += chunk; });
33+
res.on('end', function() {
34+
console.log('Successfully processed HTTPS response');
35+
context.succeed(); });
36+
});
37+
38+
req.on('error', context.fail);
39+
40+
stream=awslogsData.logStream;
41+
group=awslogsData.logGroup;
42+
43+
curRequestID = null;
44+
var re = new RegExp(/RequestId: (\S+) /);
45+
awslogsData.logEvents.forEach(function(val, idx, arr) {
46+
val.logStream = stream;
47+
val.logGroup = group;
48+
var rs = re.exec(val.message);
49+
if (rs!=null) { curRequestID = rs[1]; }
50+
val.requestID = curRequestID;
51+
req.write(JSON.stringify(val) + '\n');
52+
});
53+
req.end();
54+
});
55+
};
56+

0 commit comments

Comments
 (0)