1

I have created a Lambda function which will be triggered via a subscription to a CloudWatch Log Pattern and the function will in-turn pass the logs to a web-hook (Refer https://gist.github.com/tomfa/f4e090cbaff0189eba17c0fc301c63db).

Now, I need this lambda function to EXECUTE only if the the function is called "x" times in "y" minutes.

Is it possible to disable/enable a lambda through SNS. Another idea is to 1. Create CloudWatch Events on State Change 2. Subscribe this to a SNS which will enables the lambda, if state goes from OK to ALARM disables the lambda, if state goes back to OK

Mohan Krishnan
  • 353
  • 3
  • 16

2 Answers2

1

You can use CloudWatch Events to send a message to an Amazon SNS topic on a schedule. make sure you are in correct region as as CloudWatch Events is not available in every region.

How to configure Cloudwatch :

Use CloudWatch and get metrics about the lambda invocation and error and you can find successful call and error , threshold count. now you can use AWS SDK

https://docs.aws.amazon.com/cli/latest/reference/cloudwatch/get-metric-data.html

export.handler = function(event, context, callback) {
  apiCall().then(resp => callback(null, resp).catch(err => callback(err));
}
vaquar khan
  • 10,864
  • 5
  • 72
  • 96
0

You could create a custom CloudWatch Metric based of your search filter of the CloudWatch Logs

Examples of this can be found in the Amazon CloudWatch Logs User Guide

  1. Count Log Events
aws logs put-metric-filter \
  --log-group-name MyApp/access.log \
  --filter-name EventCount \
  --filter-pattern "" \
  --metric-transformations \
  metricName=MyAppEventCount,metricNamespace=MyNamespace,metricValue=1,defaultValue=0
  1. Count Occurrences
aws logs put-metric-filter \
  --log-group-name MyApp/message.log \
  --filter-name MyAppErrorCount \
  --filter-pattern 'Error' \
  --metric-transformations \
      metricName=ErrorCount,metricNamespace=MyNamespace,metricValue=1,defaultValue=0

Then you can go in and create a CloudWatch Alarm that will fire based on x of these events being logged in y time span. The CloudWatch Alarm can send a message to an SNS topic that triggers your Lambda function

Dude0001
  • 3,019
  • 2
  • 23
  • 38