將警示組態建立為 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 取代為您的值。
呼叫 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
StartTime 和 EndTime 的支援 DateTime 格式為 2020-01-01T00:00:00Z。
**重要事項:**上述命令會根據每個 GetMetricData API 呼叫擷取的指標數量產生費用。如需詳細資訊,請參閱 Amazon CloudWatch 定價。