Create an alarm configuration as a JSON file.
Example JSON file:
$ 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
}]
}
Note: Replace the REGION, ACCOUNT_ID, and SNS_TOPIC_NAME with your values.
Call the PutMetricAlarm API.
Example API call:
aws cloudwatch put-metric-alarm --cli-input-json file://alarm_config.json
(Optional) To find the previous day's top ten contributors to the Maximum DailyEstimatedCharges value, run the following query:
$ 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
The supported DateTime format for StartTime and EndTime is 2020-01-01T00:00:00Z.
Important: The preceding command incurs charges based on the number of metrics that each GetMetricData API call retrieves. For more information, see Amazon CloudWatch Pricing.