【问题标题】:How do I setup an alarm for CloudFront from CloudWatch in a CloudFormation yaml template?如何在 CloudFormation yaml 模板中从 CloudWatch 为 CloudFront 设置警报?
【发布时间】:2018-11-27 06:58:45
【问题描述】:

我想设置警报,以防 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

【问题讨论】:

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


    【解决方案1】:

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

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

    作为第二步,您需要将DistributionId 提供给 CF 模板(只要 Distribution 不是 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
    

    【讨论】:

    • 我收到此错误:User is not authorized to perform: SNS:CreateTopic on resource(Service: AmazonSNS; Status Code: 403; Error Code: AuthorizationError。我必须添加策略吗?如果是在哪里?
    • 您作为用户似乎没有这样做的权限。
    • 如何更改?我想更改 cloudFormation 中的权限。这可能吗?
    • 您必须要求帐户所有者授予您的帐户执行此操作的权限。
    • 帮我解答
    猜你喜欢
    • 1970-01-01
    • 2017-02-20
    • 2019-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多