AWS CloudWatch log subscription filters decode

2020-07-09 07:08发布

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);
};

1条回答
再贱就再见
2楼-- · 2020-07-09 07:13

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:

const zlib = require('zlib');

exports.handler = async (event) => {
  if (event.awslogs && event.awslogs.data) {
    const payload = Buffer.from(event.awslogs.data, 'base64');

    const logevents = JSON.parse(zlib.unzipSync(payload).toString()).logEvents;

    for (const logevent of logevents) {
      const log = JSON.parse(logevent.message);
      console.log(log);
    }
  }
};
查看更多
登录 后发表回答