如何监控每日 EstimatedCharges 并基于我的使用阈值触发 CloudWatch 警报?
上次更新时间:2020 年 9 月 14 日
如何监控每日 EstimatedCharges 并基于我的使用阈值触发 Amazon CloudWatch 警报?
简短描述
EstimatedCharges 指标大约每隔六小时发布一次。结果将每月重置一次。MetricMath RATE 表达式的计算方法为,将最新数据点值与上一个数据点值之间的差值除以两个值之间的时间差(以秒为单位)。您可以使用此表达式来计算每天的 EstimatedCharges 值。
解决方法
注意:如果您在运行 AWS 命令行界面 (AWS CLI) 命令时收到错误,请确保您使用的是最新的 AWS CLI 版本。
测试 RATE 表达式并验证输出
在创建 CloudWatch 警报之前,请调用 GetMetricData API 来测试 RATE 表达式:
$ cat metric_data_queries.json
{
"MetricDataQueries": [
{
"Id": "m1",
"MetricStat": {
"Metric": {
"Namespace": "AWS/Billing",
"MetricName": "EstimatedCharges",
"Dimensions": [
{
"Name": "Currency",
"Value": "USD"
}
]
},
"Period": 86400,
"Stat": "Maximum"
}
},
{
"Id": "e1",
"Expression": "RATE(m1) * 86400",
"Label": "DailyEstimatedCharges",
"Period": 86400
}
],
"StartTime": "2020-06-01T00:00:00Z",
"EndTime": "2020-06-05T00:00:00Z",
"ScanBy": "TimestampAscending"
}
$ aws cloudwatch get-metric-data --cli-input-json file://metric_data_queries.json
{
"MetricDataResults": [
{
"Id": "m1",
"Label": "EstimatedCharges",
"Timestamps": [
"2020-06-01T00:00:00Z",
"2020-06-02T00:00:00Z",
"2020-06-03T00:00:00Z",
"2020-06-04T00:00:00Z"
],
"Values": [
0.0,
22.65,
34.74,
46.91
],
"StatusCode": "Complete"
},
{
"Id": "e1",
"Label": "DailyEstimatedCharges",
"Timestamps": [
"2020-06-02T00:00:00Z",
"2020-06-03T00:00:00Z",
"2020-06-04T00:00:00Z"
],
"Values": [
22.65,
12.090000000000003,
12.169999999999995
],
"StatusCode": "Complete"
}
],
"Messages": []
}
输出表:
时间戳 | 2020-06-01T00:00:00Z | 2020-06-02T00:00:00Z | 2020-06-03T00:00:00Z | 2020-06-04T00:00:00Z |
---|---|---|---|---|
EstimatedCharges | 0.0 | 22.65 | 34.74 | 46.91 |
DailyEstimatedCharges | --- | 22.65 | 12.09 | 12.17 |
在输出表中,确认 DailyEstimatedCharges 正确计算为最新数据点与上一个数据点之间的差值。您可以使用此表达式创建您的 CloudWatch 警报。
使用 AWS 管理控制台创建 CloudWatch 警报
1. 按照基于指标数学表达式创建 CloudWatch 警报中的步骤执行操作。
2. 将以下代码粘贴到“CloudWatch 指标”页面的源选项卡中。此代码将创建 MetricMath 表达式 [ RATE(m1) * 86400 ],它将 EstimatedCharges 用作基本指标,并且具有标签“m1”。
{
"metrics": [
[ { "expression": "RATE(m1) * 86400", "label": "Expression1", "id": "e1", "period": 86400, "stat": "Maximum" } ],
[ "AWS/Billing", "EstimatedCharges", "Currency", "USD", { "id": "m1" } ]
],
"view": "timeSeries",
"stacked": false,
"region": "us-east-1",
"stat": "Maximum",
"period": 86400
}
3. 为 MetricMath 表达式创建 CloudWatch 警报。要执行此操作,请选择已绘制指标。在 DailyEstimatedCharges 指标的操作列中,选择警报图标。
4. 在 CloudWatch 警报创建向导中:
确认指标配置的详细信息。
添加适当的阈值,以便在超出该阈值时接收通知(例如 50 USD)。
配置警报操作。
添加警报的名称和描述。
使用 AWS CLI 创建 CloudWatch 警报
1. 将警报配置创建为 JSON 文件:
$ cat alarm_config.json
{
"AlarmName": "DailyEstimatedCharges",
"AlarmDescription": "This alarm would be triggered if the daily estimated charges exceeds 50$",
"ActionsEnabled": true,
"AlarmActions": [
"arn:aws:sns:<REGION>:<ACCOUNT_ID>:<SNS_TOPIC_NAME>"
],
"EvaluationPeriods": 1,
"DatapointsToAlarm": 1,
"Threshold": 50,
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"TreatMissingData": "breaching",
"Metrics": [{
"Id": "m1",
"MetricStat": {
"Metric": {
"Namespace": "AWS/Billing",
"MetricName": "EstimatedCharges",
"Dimensions": [{
"Name": "Currency",
"Value": "USD"
}]
},
"Period": 86400,
"Stat": "Maximum"
},
"ReturnData": false
},
{
"Id": "e1",
"Expression": "RATE(m1) * 86400",
"Label": "DailyEstimatedCharges",
"ReturnData": true
}]
}
注意:请务必使用相应的值更新 REGION、ACCOUNT_ID 和 SNS_TOPIC_NAME。
2. 调用 PutMetricAlarm API:
aws cloudwatch put-metric-alarm --cli-input-json file://alarm_config.json
(可选)查找前一天的最大 DailyEstimatedCharges 值的前十大贡献因素
使用以下查询:
$ cat top_contributors_query.json
{
"MetricDataQueries": [{
"Id": "e1",
"Expression": "SORT(RATE(SEARCH('{AWS/Billing,Currency,ServiceName} AWS/Billing ServiceName', 'Maximum', 86400))*86400, MAX, DESC, 10)",
"Label": "DailyEstimatedCharges",
"Period": 86400
}],
"ScanBy": "TimestampAscending"
}
$ aws cloudwatch get-metric-data --cli-input-json file://top_contributors_query.json --start-time `date -v -2d '+%Y-%m-%dT%H:%M:%SZ'` --end-time `date '+%Y-%m-%dT%H:%M:%SZ'`
注意:StartTime 和 EndTime 支持的 DateTime 格式为“2020-01-01T00:00:00Z”。
重要提示:前面的命令将根据每个 GetMetricData API 调用检索到的指标数产生费用。有关更多信息,请参阅 Amazon CloudWatch 定价。