AWS Lambda function to collector logs from CloudWatch Logs and post them to SumoLogic via a HTTP collector endpoint
We recommend using SumoLogic Lambda Function for AWS CloudWatch Logs With Dead Letter Queue Support as it is configured with Dead Letter Queue which takes care of messages that can't be processed (consumed) successfully.
First create an HTTP collector endpoint within SumoLogic. You will need the endpoint URL for the lambda function later.
- Within the AWS Lambda console select create new Lambda function
- Select
Blank Function
on the select blueprint page - Leave triggers empty for now, click next
- Configure Lambda
- Select Node.js 10.x as runtime
- Copy code from cloudwatchlogs_lambda.js into the Lambda function code.
- Add Environment variables (See below)
- 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. - Finally click on "Create function" to create the function.
- (Optional) Test this new function with sample AWS CloudWatch Logs template provided by AWS
- Within the AWS CloudWatch Logs console, check the Log Group you want to send data to Sumologic.
- From Actions button, select "Stream to AWS Lambda".
- Select Lambda function created above.
- Select
json
as the log format and define any filters. - Click start streaming.
The following AWS Lambda environment variables are supported
SUMO_ENDPOINT
(REQUIRED) - SumoLogic HTTP Collector endpoint URL.ENCODING
(OPTIONAL) - Encoding to use when decoding CloudWatch log events. Default is 'utf-8'.SOURCE_CATEGORY_OVERRIDE
(OPTIONAL) - Override _sourceCategory metadata field within SumoLogic. Ifnone
will not be overriddenSOURCE_HOST_OVERRIDE
(OPTIONAL) - Override _sourceHost metadata field within SumoLogic. Ifnone
will not be overriddenSOURCE_NAME_OVERRIDE
(OPTIONAL) - Override _sourceName metadata field within SumoLogic. Ifnone
will not be overriddenLOG_STREAM_PREFIX
(OPTIONAL) - Comma separated list of logStream name prefixes to filter by logStream, especially for AWS Batch logs
The lambda supports dynamically overriding the _sourceName, _sourceHost and _sourceCategory per log message by setting _sumo_metadata
within a json log.
This can be useful when writing to CloudWatch Logs via a lambda function.
For example:
exports.handler = (event, context, callback) => {
var serverIp = '123.123.123.123'
console.log(JSON.stringify({
'message': 'something happened..',
'_sumo_metadata': {
'category': 'prod/appa/console',
'source': 'other_source',
'host': serverIp
}
}));
console.log('some other log message with default sourceCategory');
};