0
votes

I created a lambda function to send an SNS notification every time it matches a particular keyword from cloudwatch logs.This is the filter pattern where the lambda function gets triggered

The only problem here is lambda only sends a mail once when a particular keyword is found in cloudwatch logs even if there are similar words in the logs repeating multiple times. I want it to send an SNS mail 5 times if it finds the same word 5 times.

Please let me know if this is understandable.

This is my whole lambda structure and This is the lambda code

Thanks! Code I use is python

1

1 Answers

1
votes

If multiple logs match the filter at the time of a lambda invocation the lambda will only be executed once, but all the logs will be included in the input. In your lambda event.awslogs.data contains base64-encoded gzipped JSON with a logEvents property that is an array whose length is the number of logs that matched the filter.

If you want to publish once for each log, you need to loop through that array and publish once to SNS for each log entry. In Node.js that would look something like this:

const util = require( 'util' );
const zlib = require( 'zlib' );
const AWS = require( 'aws-sdk' );

const gunzip = util.promisify( zlib.gunzip );
const sns = new AWS.SNS( );

exports.handler = async function ( event ) {
    const gzippedPayload = Buffer.from( event.awslogs.data, 'base64' );
    const payload = await gunzip( gzippedPayload );
    const { logEvents } = JSON.parse( payload.toString( 'utf8' ) );

    if ( ! logEvents )
      return;

    for ( const log of logEvents ) {
      await sns.publish( {
        TopicArn: process.env.SNS_TOPIC_ARN,
        Message: "if you get this message you can go back to sleep!",
      } ).promise( );
    }
};