如何在 CloudFormation yaml 模板中为来自 CloudWatch 的 CloudFront 设置警报?
How do I setup an alarm for CloudFront from CloudWatch in a CloudFormation yaml template?
我想设置一个警报,以防 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
首先,您需要创建一个 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
(只要 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
"final" 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
我想设置一个警报,以防 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
首先,您需要创建一个 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
(只要 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
"final" 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