Lukas Z's Blog

Setting AWS APIGateway Alarms for Certain URLs Only

Quick post on how to set Alarms in AWS CloudWatch that trigger for certain URLs only. This assumes you use API Gateway.

Bascially, you can set Alarms in 3 ways only: Either on the entire API, on the API and the Stage OR on API, Stage, Resource and Method. For the latter two Enable Detailed CloudWatch Metrics has to be checked in the Logs section of Stage properties.

That’s basically it. You need all four dimensions if you want to filter by URL and you need enable Cloudwatch metrics on the stage.

Here’s a YAML snippet showing the declaration of an alarm.

Type: "AWS::CloudWatch::Alarm"
  ActionsEnabled: true
  AlarmActions: [!ImportValue {"Fn::Sub": "SomeNotificationTopicArn"}]
  AlarmDescription: "Some Description"
  AlarmName: "alarm-name"
  ComparisonOperator: GreaterThanThreshold
    - Name: ApiName
      Value: "my-api"
    - Name: Stage
      Value: "LATEST"
    - Name: Resource
      Value: "/some_resource/{somePathParameter}/{anotherPathParameter}"
    - Name: Method
      Value: "GET"
  EvaluationPeriods: 1
  MetricName: 5XXError
  Namespace: AWS/ApiGateway
  OKActions: [!ImportValue {"Fn::Sub": "SomeNotificationTopicArn"}]
  Period: 60
  Statistic: Sum
  Threshold: 0
  TreatMissingData: notBreaching

(I didn’t check the YAML but it should be fine.)

P.S.: You can follow me on Twitter.