具有异常检测功能的 CloudWatch 警报的 AWS-CDK 示例

AWS-CDK Example of CloudWatch Alarm with Anomaly detection

有人可以提供一个基于异常检测的 AWS Cloudwatch 警报的通用示例吗?Python3 中使用 AWS CDK 构建的指标?

我曾尝试将 aws cloudformation example of it 映射到 aws_cdk.aws_cloudwatch.Alarmaws_cdk.aws_cloudwatch.CfnAlarm,但运气不佳。

当前 CDK(v1.27.0) 中没有 AbnormalDetector 的高级构造函数。

请直接使用低电平class CfnAnomalyDetector.

CloudFormation 文档中有一个 example usage of AbnormalDetector with Alarm。使用 CDK low level Cfn* classes 创建相应的 CFN 配置。

最终设法使用 CfnAlarm 进行异常检测

from aws_cdk import (
    aws_sns as sns,
    aws_cloudwatch as cw,
    core
)

anomaly_detector = cw.CfnAnomalyDetector(
    self, "AnomalyDetector",
    metric_name="my_metric",
    namespace="my_namespace",
    stat="Sum"
)

slack_topic = sns.Topic(self, "AnomalySns",
                        display_name="anomaly-sns",
                        topic_name="anomaly-sns"
                        )

anomaly_cfnalarm = cw.CfnAlarm(self, "AnomalyAlarm",
                               actions_enabled=True,
                               alarm_actions=[slack_topic.topic_arn],
                               alarm_description="<..>",
                               alarm_name="AnomalyAlarm",
                               comparison_operator="LessThanLowerOrGreaterThanUpperThreshold",
                               datapoints_to_alarm=1,
                               evaluation_periods=1,
                               insufficient_data_actions=[slack_topic.topic_arn],
                               metrics=[
                                   cw.CfnAlarm.MetricDataQueryProperty(
                                       expression="ANOMALY_DETECTION_BAND(m1, 2)",
                                       id="ad1"
                                   ),
                                   cw.CfnAlarm.MetricDataQueryProperty(
                                       id="m1",
                                       metric_stat=cw.CfnAlarm.MetricStatProperty(
                                           metric=cw.CfnAlarm.MetricProperty(
                                               metric_name=anomaly_detector.metric_name,
                                               namespace=anomaly_detector.namespace
                                           ),
                                           period=core.Duration.minutes(5).to_seconds(),
                                           stat="Sum"
                                       )
                                   )
                               ],
                               ok_actions=[slack_topic.topic_arn],
                               threshold_metric_id="ad1",
                               treat_missing_data="breaching"
                               )

这部分基于 Cloudwatch 异常警报的 AWS Docs CloudFormation 示例 https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html