无法在 Cloudformation 模板中为 Cloudwatch 警报定义数学表达式
Unable to define Math Expression for Cloudwatch Alarm in a Cloudformation Template
Recently AWS announced that Cloudwatch alarms can use Math Expressions on metrics. I decided to create an alarm that compares the SUM of 2 single metrics with a given threshold. This means that according to AWS documentation 我的表达式应该是 SUM([m1,m2]),其中 m1 和 m2 是 2 个单一指标。
我还决定使用 cloudformation 模板(在 yaml 中)来实现这个想法。以下是 Cloudwatch 警报定义:
BillingAlarmExpression:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmActions:
- !Ref BillingAlertTopic
AlarmDescription: String
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
Metrics:
- Id: m1
MetricStat:
Metric:
Dimensions:
- Name: ServiceName
Value: AmazonEC2
- Name: Currency
Value: USD
MetricName: EstimatedCharges
Namespace: AWS/Billing
Period: 86400
Stat: Maximum
ReturnData: False
- Id: m2
MetricStat:
Metric:
Dimensions:
- Name: ServiceName
Value: AmazonCloudwatch
- Name: Currency
Value: USD
MetricName: EstimatedCharges
Namespace: AWS/Billing
Period: 86400
Stat: Maximum
ReturnData: False
- Id: Expr1
Expression: SUM([m1,m2])
Label: Yeap
Threshold: 100
TreatMissingData: ignore
单个指标 m1 和 m2 与 EC2 和 Cloudwatch 服务的计费成本有关。我想检查的是这两项服务的收费是否超过了 100 美元的门槛。 (请注意,由于计费费用专门存储在 N.Virginia 区域,因此我尝试在 N.Virginia 中部署上述模板)。
在部署此模板期间,Cloudformation 响应以下错误:
"Invalid metrics list (Service: AmazonCloudWatch; Status Code: 400; Error Code: ValidationError; Request ID: c0748047-0378-11e9-ac36-5b1829988d18)"
当 Cloudformation 说 "metrics list" 时,它指的是 m1、m2、Expr1 的定义。更奇怪的是,当我使用 aws cli 中的上述指标列表定义时,收费数据 return 成功:
aws cloudwatch get-metric-data --metric-data-queries file://./metric-data.json --start-time 2018-12-03T03:00:00Z --end-time 2018-12-10T04:30:00Z
,其中 metric-data.json 是上面的 metrics-list.
为了创建我的模板,我使用了以下指南:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html
和
https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricData.html
您知道为什么 Cloudformation return 会出现这个错误吗?谢谢!
Id
必须以小写字母开头,将Expr1
改为expr1
。
来自docs:
You can change the value of Id. It can include numbers, letters, and underscore, and must start with a lowercase letter.
我也 运行 关注这个问题——如果你的问题不是公认的答案(id
使用小写),这些事情也值得关注可能导致错误
- 确保您遵循
MetricDataQuery
的正确形状,并注意哪些属性实际上是可选的,哪些不是
- 确保您正在编写的数学表达式是
String
(用引号引起来)
- 确保您使用的是正确的
MetricStat
值(使用 Max
而不是 Maximum
会引发此错误)
我真的希望 AWS 会考虑为此添加更好的错误消息。 Invalid Metrics list
不是很有帮助:/
Recently AWS announced that Cloudwatch alarms can use Math Expressions on metrics. I decided to create an alarm that compares the SUM of 2 single metrics with a given threshold. This means that according to AWS documentation 我的表达式应该是 SUM([m1,m2]),其中 m1 和 m2 是 2 个单一指标。 我还决定使用 cloudformation 模板(在 yaml 中)来实现这个想法。以下是 Cloudwatch 警报定义:
BillingAlarmExpression:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmActions:
- !Ref BillingAlertTopic
AlarmDescription: String
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
Metrics:
- Id: m1
MetricStat:
Metric:
Dimensions:
- Name: ServiceName
Value: AmazonEC2
- Name: Currency
Value: USD
MetricName: EstimatedCharges
Namespace: AWS/Billing
Period: 86400
Stat: Maximum
ReturnData: False
- Id: m2
MetricStat:
Metric:
Dimensions:
- Name: ServiceName
Value: AmazonCloudwatch
- Name: Currency
Value: USD
MetricName: EstimatedCharges
Namespace: AWS/Billing
Period: 86400
Stat: Maximum
ReturnData: False
- Id: Expr1
Expression: SUM([m1,m2])
Label: Yeap
Threshold: 100
TreatMissingData: ignore
单个指标 m1 和 m2 与 EC2 和 Cloudwatch 服务的计费成本有关。我想检查的是这两项服务的收费是否超过了 100 美元的门槛。 (请注意,由于计费费用专门存储在 N.Virginia 区域,因此我尝试在 N.Virginia 中部署上述模板)。 在部署此模板期间,Cloudformation 响应以下错误:
"Invalid metrics list (Service: AmazonCloudWatch; Status Code: 400; Error Code: ValidationError; Request ID: c0748047-0378-11e9-ac36-5b1829988d18)"
当 Cloudformation 说 "metrics list" 时,它指的是 m1、m2、Expr1 的定义。更奇怪的是,当我使用 aws cli 中的上述指标列表定义时,收费数据 return 成功:
aws cloudwatch get-metric-data --metric-data-queries file://./metric-data.json --start-time 2018-12-03T03:00:00Z --end-time 2018-12-10T04:30:00Z
,其中 metric-data.json 是上面的 metrics-list.
为了创建我的模板,我使用了以下指南: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html 和 https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricData.html
您知道为什么 Cloudformation return 会出现这个错误吗?谢谢!
Id
必须以小写字母开头,将Expr1
改为expr1
。
来自docs:
You can change the value of Id. It can include numbers, letters, and underscore, and must start with a lowercase letter.
我也 运行 关注这个问题——如果你的问题不是公认的答案(id
使用小写),这些事情也值得关注可能导致错误
- 确保您遵循
MetricDataQuery
的正确形状,并注意哪些属性实际上是可选的,哪些不是 - 确保您正在编写的数学表达式是
String
(用引号引起来) - 确保您使用的是正确的
MetricStat
值(使用Max
而不是Maximum
会引发此错误)
我真的希望 AWS 会考虑为此添加更好的错误消息。 Invalid Metrics list
不是很有帮助:/