AWS CloudWatch to SNS handler for lambda
Assuming you already have created cloudwatch log group/stream, SNS topic and lambda with role permissions for SNS publishing...
-
Create node project and add dependency:
npm install cwlogs2sns
-
Create
index.js
file with following content:
const cwlogs2sns = require("cwlogs2sns");
exports.handler = cwlogs2sns();
-
Zip your project file and upload it to AWS Lambda.
-
Setup environment variables:
AWS_SNS_TOPIC
-arn
of SNS topic you want publish toTRIGGER_WORDS
- this lambda function publish only logs that contain these trigger words, you can use*
to forward all (default:Error,Failure,Exception,UnhandledPromiseRejection
)
cwlogs2sns(options)
: lambda handler function
options
:
map(logEvent, logGroup, logStream)
: optional function for transforming logs and custom filtering (return null to filter the log)
When you setup this lambda, you can subscribe for the AWS SNS. For example, you can use sns2slack to forward messages to you slack channels.