I want to set up cloud watch alarms to fire when there is no activity, for example, to fire a cloud watch alarm when a Lambda function does NOT execute for at least 5 minutes. I set up a simple test lambda function (testLambdaFunc), and then set up an alarm using a python script as follows:
import boto3
lambdaFunction = 'testLambdaFunc'
alarmName = 'testLambdaAlarm'
client = boto3.client("cloudwatch")
# create alarm to fire after five minutes of inactivity
response = client.put_metric_alarm(
AlarmName=alarmName,
AlarmActions=[],
MetricName='Invocations',
Namespace='AWS/Lambda',
Dimensions=[
{
'Name': 'FunctionName',
'Value': lambdaFunction
},
],
Statistic='Average',
Period=300,
EvaluationPeriods=1,
Threshold=0,
ComparisonOperator='LessThanOrEqualToThreshold'
)
Immediately after creating the alarm it goes into INSUFFICIENT DATA state. Then I trigger the lambda function once to get a data point. The alarm goes into OK state and then about 10 minutes later goes back to INSUFFICIENT DATA state. Is it normal or is it supposed to go to alarm? How can I set up an alarm that fires when there is no activity on the function?
When a a CloudWatch metric does not have data for 5 or 10 minutes, any alarms will go into "INSUFFICIENT_DATA" state. This is because the alarm does not have enough data to know if it should be in "ALARM" state or "OK" state.
When you create a CloudWatch alarm, you can specify an SNS topic to notify when the alarm goes into "INSUFFICIENT_DATA" state. This is done as part of the InsufficientDataActions
member of your put_metric_alarm
method call.
If you are expecting your metric to always have data within the past 5 minutes, then you can use the InsufficientDataActions
to trigger an alert when there's not enough data. Essentially telling you that you're not getting data. I think this is what you want.