跳至内容

如何监控每日 EstimatedCharges 并根据我的使用阈值激活 CloudWatch 警报?

2 分钟阅读
0

我想要监控每日的 EstimatedCharges,并根据我的使用阈值激活 Amazon CloudWatch 警报。

简短描述

EstimatedCharges 指标大约每 6 小时发布一次。结果每月重置一次。使用 MetricMath RATE 表达式来计算每天的 EstimatedCharges 值。要计算 MetricMath RATE,请除以最新数据点值与前一个数据点值之间的差值。然后,除以两个值之间的时间差(以秒为单位)。

解决方法

**注意:**如果在运行 AWS 命令行界面(AWS CLI)命令时收到错误,请参阅 Troubleshoot AWS CLI errors。此外,请确保使用的是最新版本的 AWS CLI。

使用 AWS 管理控制台创建 CloudWatch 警报

完成下面的步骤:

  1. 根据指标数学表达式创建 CloudWatch 警报。
  2. 在导航窗格中的指标下,选择所有指标。
  3. 在指标页面上,在来源选项卡中输入以下代码:
    {    
        "metrics": [
            [ { "expression": "IF(RATE(m1)>0,RATE(m1)*86400,0)", "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
    }
    注意:前面的代码创建了指标数学表达式 [ IF(RATE(m1)>0,RATE(m1)*86400,0) ],其使用使用EstimatedCharges 作为标签为“m1”的基本指标。
  4. 要为指标数学表达式创建 CloudWatch 警报,请选择绘成图表的指标选项卡。
  5. 在 DailyEstimatedCharges 指标的操作列中,选择警报图标。
  6. 在 CloudWatch 警报创建向导中,完成下面的步骤:
    确认指标配置的详细信息。
    添加适当的阈值,以便在超过阈值时接收通知,例如 50.00 美元。
    配置警报操作。
    添加警报名称和描述。

使用 AWS CLI 创建 CloudWatch 警报

完成下面的步骤:

  1. 以 JSON 文件形式创建警报配置。
    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": "IF(RATE(m1)>0,RATE(m1)*86400,0)",
            "Label": "DailyEstimatedCharges",
            "ReturnData": true
        }]
    }

    **注意:**将 REGION、ACCOUNT_ID 和 SNS_TOPIC_NAME 替换为您的值。

  2. 调用 PutMetricAlarm API。
    API 调用示例:

    aws cloudwatch put-metric-alarm --cli-input-json file://alarm_config.json

    (可选)要查找前一天对 Maximum DailyEstimatedCharges 值贡献最大的 10 个因素,请运行以下查询:

    $ 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'` --region us-east-1

    StartTime 和 EndTime 支持的 DateTime 格式是 2020-01-01T00:00:00Z。

    **重要事项:**上述命令将根据每次 GetMetricData API 调用检索的指标数量收取费用。有关更多信息,请参阅 Amazon CloudWatch 定价。

AWS 官方已更新 2 年前