Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudWatch alarm for invocation of Lambdas with scheduling periods > 1 day

I have a Lambda that is triggered to run every week, and I want to have a CloudWatch alarm if it ever does not run for more than 7 consecutive days.

My thinking was Alarm if < 1 invocation for 8 days but it does not seem to be possible to set it longer than 24 hours:

The alarm evaluation period (number of datapoints times the period of the metric) must be no longer than 24 hours.

Is there another way to ensure execution of Lambdas that are triggered on a period of greater than 24 hours?

like image 443
alexroussos Avatar asked Oct 16 '17 18:10

alexroussos


People also ask

Can AWS CloudWatch alarm trigger Lambda?

You can use a CloudWatch Events rule that matches on alarm evaluation changes and then triggers a Lambda function that parses the alarm event and creates a customized notification.


1 Answers

Maximum evaluation period is 24 hours.

You can get around that by creating a custom metric using CloudWatch PutMetricData API. You can publish the time elapsed since the last execution of your lambda function and then alarm when the value rises above 8 days.

One way of doing this would be to have your lambda function store the timestamp of execution to DynamoDB every time it triggers. Then you can create a new function that will read that timestamp from DynamoDB and publish the difference between it and current time to a custom metric (have that lambda trigger every 1h for example).

Once you have the new custom metric flowing, you can create an alarm that will fire if the value goes above 8 days for one 1h datapoint (this will solve your initial issue). You can also set the Treat missing data as option to bad - breaching threshold (this will alert you if the second lambda function doesn't trigger).

You should also set alarms on CloudWatch Events errors and Lambda errors. This will alert you if something goes wrong with the scheduling or the lambda itself. But the custom metric I mentioned above will also alert you in the case of human error where someone disables or deletes the event or the function by mistake for example.

like image 110
Dejan Peretin Avatar answered Oct 10 '22 22:10

Dejan Peretin