Search code examples
amazon-web-servicesaws-cloudformationcloudwatch-alarms

Cloud Formation for Cloud Watch Alarms on metrics generated by CW Agent


I have installed CW Agent on multiple EC2 instances to fetch the storage used. I'm getting disk_used_percent metrics from multiple instances like this,

enter image description here

Since the name of the metric is same for all the instances, If I create a CW alarm from the console I will click on metric and will create an alarm but I'm trying to do it from Cloud Formation script. In CF script there is only an option for Namespace which will be CWAgent in my case and metric name which will be disk_used_percent. My question is how can I define which instance metric I want to fetch, Here are the screenshots for manually created alarm and CF created alarm,

Manually Created

CF Created

How to define instance_id and other parameters in CF script? I can't find any of these option on CW CF sample scripts on AWS website.

  CloudWatchAlarm:
  Type: "AWS::CloudWatch::Alarm"
  Properties:
    AlarmName: "disk-space-threshold"
    AlarmDescription: "A Cloudwatch Alarm that triggers when disk space of EBS is less than 50%"
    MetricName: "disk_used_percent"
    Namespace: "CWAgent"
    Statistic: "Average"
    Period: "60"
    EvaluationPeriods: "1"
    Threshold: "1"
    ComparisonOperator: "GreaterThanOrEqualToThreshold"
    TreatMissingData: "notBreaching"

Solution

  • How to define instance_id and other parameters in CF script?

    You need to add Dimensions to your AWS::CloudWatch::Alarm. InstanceId is one of the dimensions of your disk_used_percent metric.