【问题标题】:How to set alarm in metric filter using CloudFormation如何使用 CloudFormation 在指标过滤器中设置警报
【发布时间】:2021-08-02 06:18:48
【问题描述】:

如何为指标过滤器创建警报并连接 SNS 主题。

我的任务是:

  1. 创建 SNS 主题和电子邮件订阅
  2. 创建 CloudWatch 日志组
  3. 为该 CloudWatch 日志组创建指标过滤器并创建警报并连接 SNS 主题
AWSTemplateFormatVersion: "2010-09-09"
#Parameters:
 #EmailAddress:
  #Type: String
  #Description: The email address to use for alarm notifications.

Resources:
 # Create SNS and email subscription
 MySNSTopic:
  Type: AWS::SNS::Topic
  Properties:
    Subscription:
    - Endpoint: "abcd@abc.com"
      Protocol: email
   
 # Create CloudWatch log group
 snstopic: 
  Type: AWS::Logs::LogGroup
  DependsOn: MySNSTopic
  Properties: 
    RetentionInDays: 7
  
 # Create metric filter
 UnauthorizedApiCalls: 
  Type: AWS::Logs::MetricFilter
  DependsOn: snstopic
  Properties: 
    LogGroupName: 
      Ref: "snstopic"
    FilterPattern: '{($.errorCode="*UnauthorizedOperation") || ($.errorCode="AccessDenied*")}'
    MetricTransformations: 
      - MetricValue: "1"
        MetricNamespace: "unauthorized-api-calls"
        MetricName: "LogMetrics"
  
 # Create alarm
 UnauthorizedApiCallsAlarm:
  Type: AWS::CloudWatch::Alarm
  DependsOn: UnauthorizedApiCalls
  Properties:
    AlarmName: UnauthorizedApiCallsAlarm
    AlarmActions: !Ref snstopic
    MetricName: UnauthorizedApiCalls
    Namespace: LogMetrics
    ComparisonOperator: GreaterThanOrEqualToThreshold
    EvaluationPeriods: '1'
    Period: '5'
    Statistic: Sum
    Threshold: '1'
    TreatMissingData: breaching

【问题讨论】:

  • 你试过这个cfn yaml吗?您面临的问题是什么?
  • 不在 cloudwatch 日志组中创建警报。
  • 我认为我的“创建警报”yaml 代码是错误的。你能解决这个问题吗?
  • 为什么你认为它错了?到底发生了什么?有任何错误信息吗?

标签: amazon-web-services amazon-cloudformation alarm


【解决方案1】:

试试这个。根据documentation,AlarmActions的类型为List of String。

# Create alarm
UnauthorizedApiCallsAlarm:
 Type: AWS::CloudWatch::Alarm
 DependsOn: UnauthorizedApiCalls
 Properties:
   AlarmName: UnauthorizedApiCallsAlarm
   AlarmActions: 
   - !Ref snstopic
   MetricName: UnauthorizedApiCalls
   Namespace: LogMetrics
   ComparisonOperator: GreaterThanOrEqualToThreshold
   EvaluationPeriods: '1'
   Period: '5'
   Statistic: Sum
   Threshold: '1'
   TreatMissingData: breaching

【讨论】:

  • 这个“ROLLBACK_COMPLETE”。无法创建堆栈。我该怎么办?
  • 1) 创建 SNS 主题和电子邮件订阅 2) 创建 cloudwatch 日志组 3) 为该 cloudwatch 日志组创建指标过滤器并创建警报并连接 sns 主题。这些是我的主要要求。
  • ROLLBACK_COMPLETE 的错误信息是什么?请提供所有错误消息。
猜你喜欢
  • 2021-08-14
  • 2018-12-14
  • 1970-01-01
  • 2020-10-25
  • 2022-11-29
  • 2018-11-27
  • 2019-08-17
  • 2018-04-04
  • 2019-07-21
相关资源
最近更新 更多