Reputation: 686
I have an app with a bunch of Lambdas. Instead of adding one CloudWatch alarm for each is there a way to combine all of them into one alarm that goes off if any of the Lambdas error out?
So far I tried:
Upvotes: 4
Views: 3531
Reputation: 513
I came across this page while looking for a solution and learned about composite alarms from a comment above. So to pay it forward, here's some sample code in Python that I used to get the composite alarm working. I don't know if this is the best way to do this, but it works for my use case.
from aws_cdk import aws_cloudwatch as cw
lambda_alarms = {}
for lambda_def in lambda_defs:
lambda_name = lambda_def['name']
lambda_object = _lambda.Function()
error_metric = lambda_object.metric_errors(
period=aws_cdk.Duration.minutes(5),
statistic="Sum",
label="Total errors past five minutes"
)
lambda_alarms[lambda_name] = cw.Alarm(self, f"{lambda_name}-alarm",
comparison_operator=cw.ComparisonOperator.GREATER_THAN_THRESHOLD,
threshold=10,
evaluation_periods=1,
metric=error_metric
)
alarm_rule = cw.AlarmRule.any_of(*lambda_alarms.values())
# the name here will show up in the email subject
composite_alarm = cw.CompositeAlarm(self, "LambdasCompositeAlarm",
alarm_rule=alarm_rule
)
topic_name = "Lambda Alarm Notifications"
error_topic = sns.Topic(self, topic_name)
email_subscription = subs.EmailSubscription('[email protected]')
error_topic.add_subscription(email_subscription)
alarm_action = cw_actions.SnsAction(
topic=error_topic
)
composite_alarm.add_alarm_action(alarm_action)
Upvotes: 1
Reputation: 1089
You should create Alarm using metric math expression on the metrics (plural) required.
In this way you can control the metrics you need and not use "All > Lambda > Across All Functions.".
Upvotes: 1
Reputation: 12089
You have your metrics per lambda function but you also have overall metrics that include data for all functions. You can just alarm on that.
You can find them in CloudWatch Console Metrics view by selecting All > Lambda > Across All Functions
These metrics don't have any dimensions, just the namespace and metric name, example source of a graph would be:
{
"metrics": [
[ "AWS/Lambda", "Errors" ]
],
"view": "timeSeries",
"stacked": false,
"region": "eu-west-1",
"stat": "Sum",
"period": 300
}
Upvotes: 1
Reputation: 686
One possible solution is to use Metrics and combine all dimensions using Expression: MAX([d1,d2,...]). Just make sure to pass 'ReturnData: false' on the other metrics.
Upvotes: 1