如何在CloudFormation yaml模板中从CloudWatch为CloudFront设置警报?

时间:2018-06-18 12:14:58

标签: amazon-web-services amazon-cloudformation amazon-cloudwatch amazon-cloudwatch-metrics

我希望在CloudWatch从CloudFront发生错误时设置警报。

在控制台中,我会直接创建一个警报,以便在TotalErrorRate大于0的情况下向我发送电子邮件。这样可以正常工作。

但现在我想在CloudFormation的yaml模板文件中设置相同的设置。我无法确定相应参数的正确值。我的文件目前看起来像这样:

  # CloudWatch
  CloudFrontTotalErrorRateAlarm:
    Type: "AWS::CloudWatch::Alarm"
    Properties:
      ActionsEnabled: Boolean
      AlarmActions:
        - String
      AlarmDescription: "Trigers an alarm if there is any error (e.g. 4xx,5xx)"
      AlarmName: "MyApiTotalErrorRate"
      ComparisonOperator: GreaterThanThreshold
      Dimensions:
        - Dimension
      EvaluationPeriods: "1"
      ExtendedStatistic: String
      InsufficientDataActions:
        - String
      MetricName: TotalErrorRate
      Namespace: AWS/CloudFront
      OKActions:
        - String
      Period: 60
      Statistic: String
      Threshold: 0
      TreatMissingData: String
      Unit: String

对于某些参数,我可以弄清楚实际值是多少。但对于其他人,我基本上不知道应该放什么,以便AWS在发生错误时向我发送电子邮件。以下参数缺少值:

  • ActionsEnabled
  • AlarmActions
  • Dimensions
  • ExtendedStatistic
  • InsufficientDataActions
  • OKActions
  • Statistic
  • TreatMissingData
  • Unit

1 个答案:

答案 0 :(得分:6)

首先,您需要使用您的电子邮件地址作为一个订阅者创建SNS Topic

EscalationTopic:
  Type: AWS::SNS::Topic

EscalationTopicEmailSubscriber:
    Type: AWS::SNS::Subscription
    Properties:
      Endpoint: john.doe@example.com
      Protocol: email
      TopicArn: !Ref EscalationTopic

作为第二步,您需要向CF模板提供DistributionId(只要分发不是CF模板的一部分):

Parameters:
  DistributionId:
    Type: String

最后,您必须将所有内容整合在一起,并按以下方式配置CloudWatch Alarm

CloudFrontTotalErrorRateAlarm:
  Type: AWS::CloudWatch::Alarm
  Properties:
    Namespace: AWS/CloudFront
    MetricName: TotalErrorRate
    Dimensions:
      - Name: DistributionId
        Value: !Ref DistributionId
    Statistic: Sum
    Period: 60
    EvaluationPeriods: 1
    ComparisonOperator: GreaterThanOrEqualToThreshold
    Threshold: 1
    AlarmActions:
      - !Ref EscalationTopic

“最终”CF模板可能如下所示:

AWSTemplateFormatVersion: 2010-09-09
Parameters:
  DistributionId:
    Type: String
Resources:
  EscalationTopic:
    Type: AWS::SNS::Topic

  EscalationTopicEmailSubscriber:
      Type: AWS::SNS::Subscription
      Properties:
        Endpoint: john.doe@example.com
        Protocol: email
        TopicArn: !Ref EscalationTopic

  CloudFrontTotalErrorRateAlarm:
    Type: AWS::CloudWatch::Alarm
    Properties:
      Namespace: AWS/CloudFront
      MetricName: TotalErrorRate
      Dimensions:
        - Name: DistributionId
          Value: !Ref DistributionId
      Statistic: Sum
      Period: 60
      EvaluationPeriods: 1
      ComparisonOperator: GreaterThanOrEqualToThreshold
      Threshold: 1
      AlarmActions:
        - !Ref EscalationTopic
相关问题