Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to define Math Expression for Cloudwatch Alarm in a Cloudformation Template

Recently AWS announced that Cloudwatch alarms can use Math Expressions on metrics. I decided to create an alarm that compares the SUM of 2 single metrics with a given threshold. This means that according to AWS documentation my expression should be SUM([m1,m2]), where m1 and m2 are 2 single metrics. I also decided to implement this idea using a cloudformation template (in yaml). Here's the Cloudwatch alarm definition:

BillingAlarmExpression:
  Type: AWS::CloudWatch::Alarm
  Properties:
    AlarmActions:
      - !Ref BillingAlertTopic
    AlarmDescription: String
    ComparisonOperator: GreaterThanOrEqualToThreshold
    EvaluationPeriods: 1
    Metrics:
      - Id: m1
        MetricStat:
          Metric:
            Dimensions:
              - Name: ServiceName
                Value: AmazonEC2
              - Name: Currency
                Value: USD
            MetricName: Estimated­Charges
            Namespace: AWS/Billing
          Period: 86400
          Stat: Maximum
        ReturnData: False
      - Id: m2
        MetricStat:
          Metric:
            Dimensions:
              - Name: ServiceName
                Value: AmazonCloudwatch
              - Name: Currency
                Value: USD
            MetricName: Estimated­Charges
            Namespace: AWS/Billing
          Period: 86400
          Stat: Maximum
        ReturnData: False
      - Id: Expr1
        Expression: SUM([m1,m2])
        Label: Yeap
    Threshold: 100
    TreatMissingData: ignore

The single metrics , m1 and m2 , have to do with the billing cost of EC2 and Cloudwatch service. What I want to check is if the charging cost for these 2 services has crossed the threshold of 100$. (Note that since the billing costs are exclusively stored in the N.Virginia region, I tried to deploy the above template in N.Virginia). During the deployment of this template Cloudformation responds with the following error:

"Invalid metrics list (Service: AmazonCloudWatch; Status Code: 400; Error Code: ValidationError; Request ID: c0748047-0378-11e9-ac36-5b1829988d18)"

When Cloudformation says "metrics list" it refers to the definition of m1,m2, Expr1. What is even more strange is that when I use the above metrics-list definition from aws cli the charging data return successfully:

aws cloudwatch get-metric-data --metric-data-queries file://./metric-data.json --start-time 2018-12-03T03:00:00Z --end-time 2018-12-10T04:30:00Z

,where metric-data.json is the above metrics-list.

For creating my template I used the following guides: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html and https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricData.html

Do you have any idea why Cloudformation returns this error? Thanks!

like image 825
P. Str Avatar asked Dec 19 '18 11:12

P. Str


1 Answers

Id must start with a lowercase letter, change Expr1 to expr1.

From docs:

You can change the value of Id. It can include numbers, letters, and underscore, and must start with a lowercase letter.

like image 87
Dejan Peretin Avatar answered Nov 18 '22 19:11

Dejan Peretin