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)