如何監控每日 EstimatedCharges,並根據我的用量臨界值啟用 CloudWatch 警示?

2 分的閱讀內容
0

我想監控每日 EstimatedCharges,並根據我的用量臨界值啟用 Amazon CloudWatch 警示。

簡短說明

EstimatedCharges 指標以大約 6 小時的間隔發佈一次。結果每個月都會重設。使用 MetricMath RATE 運算式計算每天的 EstimatedCharges 值。若要計算 MetricMath RATE,請等分最新資料點值與上一個資料點值之間的差異。然後,除以兩個值之間的時間差 (以秒為單位)。

解決方法

**注意:**如果您在執行 AWS Command Line Interface (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 美元) 時接收通知。
    設定您的警示動作。
    新增警示名稱和描述。

使用 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_IDSNS_TOPIC_NAME 取代為您的值。

  2. 呼叫 PutMetricAlarm API。
    範例 API 呼叫:

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

    (選用) 若要尋找前一天的前十大 Maximum 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'` --region us-east-1

    StartTimeEndTime 的支援 DateTime 格式為 2020-01-01T00:00:00Z

    **重要事項:**上述命令會根據每個 GetMetricData API 呼叫擷取的指標數量產生費用。如需詳細資訊,請參閱 Amazon CloudWatch 定價

AWS 官方
AWS 官方已更新 9 個月前