I am using CloudWatch log subscription filters stream to Lambda and publish a message to an SNS topic. But it will output garbled message and can't success decode.
my output:
k
%"
jVbB
If not decode will output like this:
{ "awslogs": {"data": "BASE64ENCODED_GZIP_COMPRESSED_DATA"} }
My code is below and it is using nodejs:
console.log("Loading function");
var AWS = require("aws-sdk");
exports.handler = function(event, context) {
var eventText = JSON.stringify(event, null, 2);
var decodeText = new Buffer(eventText, 'base64').toString('ascii');
console.log("Received event:", eventText);
var sns = new AWS.SNS();
var params = {
Message: decodeText,
Subject: "Test SNS From Lambda",
TopicArn: "arn:aws:sns:region:account:snsTopic"
};
sns.publish(params, context.done);
};
CloudWatch Logs are delivered to the subscribed Lambda function as a list that is gzip-compressed and base64-encoded.
Here is an example of how to decode and unzip the list of logs: