【问题标题】:AWS-CDK Example of CloudWatch Alarm with Anomaly detection带有异常检测的 CloudWatch 警报的 AWS-CDK 示例
【发布时间】:2020-06-18 06:16:02
【问题描述】:

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

我尝试将aws cloudformation example of it 映射到aws_cdk.aws_cloudwatch.Alarmaws_cdk.aws_cloudwatch.CfnAlarm,但几乎没有运气。

【问题讨论】:

    标签: python amazon-cloudwatch aws-cdk


    【解决方案1】:

    最终设法使用 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"
                                   )
    

    这部分基于 AWS Docs CloudFormation 示例 Cloudwatch Anomaly Alarm https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html

    【讨论】:

      【解决方案2】:

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

      请直接使用低级类CfnAnomalyDetector

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

      【讨论】:

      • 我认为您没有理解这个问题。我能够创建 CfnAnomalyDetector 没问题。我无法做的是创建警报或 CfnAlarm,当检测器检测到异常时会触发。
      猜你喜欢
      • 1970-01-01
      • 2014-02-14
      • 2021-05-25
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多