AWS CloudWatch log subscription filters decode

ぃ、小莉子 提交于 2019-12-10 08:40:49

问题


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:


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


来源:https://stackoverflow.com/questions/50327304/aws-cloudwatch-log-subscription-filters-decode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!