Créez une configuration d’alarme sous forme de fichier JSON.
Exemple de fichier 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
}]
}
**Remarque :**Remplacez REGION, ACCOUNT_ID et SNS_TOPIC_NAME par vos valeurs.
Appelez l’API PutMetricAlarm.
Exemple d’appel d’API :
aws cloudwatch put-metric-alarm --cli-input-json file://alarm_config.json
(Facultatif) Pour trouver les dix principaux contributeurs de la journée précédente à la valeur Maximum DailyEstimatedCharges, exécutez la requête suivante :
$ 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
Le format DateTime pris en charge pour StartTime et EndTime est 2020-01-01T00:00:00Z.
Important : la commande précédente entraîne des frais en fonction du nombre de métriques récupérées par chaque appel d’API GetMetricData. Pour en savoir plus, consultez la page Tarification Amazon Cloudwatch.