Search code examples
amazon-web-servicesamazon-cloudwatchamazon-cloudwatch-metrics

Trigger an AWS Alarm when an API Gateway invocation hits its 29 second timeout and returns a 504 error


I am new in the cloud and I have the requirement to configure CloudWatch to invoke a Lambda in case of 504 error. For that, I have written below Serverless code: But on 504 error, the code in not invoking the Alarm. In the code, I have defined 29000 milliseconds (29 seconds) threshold and any request taking time more than or equal to should invoke Alarm.

Please help me to figure out what am I missing here?

 TaskTimeoutAlarm:
  Type: AWS::CloudWatch::Alarm
  Properties:
    Namespace: "AWS/ApiGateway"
    MetricName: "Latency"
    AlarmDescription: "API Gateway timeout"
    Threshold: 29000
    Period: 300
    EvaluationPeriods: 1
    ComparisonOperator: "GreaterThanOrEqualToThreshold"
    AlarmActions:
      - arn:aws:sns:${self:provider.region}:${self:provider.awsAccountId}:${self:custom.alertSnsTopic}
    OKActions:
      - arn:aws:sns:${self:provider.region}:${self:provider.awsAccountId}:${self:custom.alertSnsTopic}
    TreatMissingData: "notBreaching"
    Statistic: "Maximum"
    Dimensions:
      - Name: environment
        Value: ${self:provider.stage}

Edited -----------

The problem was in the key-value passed in Dimensions. This is how it should be

Dimensions:
      - Name: ApiName 
        Value: dev-employee-api
      - Name: Stage
        Value: dev
  • ApiName is the name of API which you can also find in AWS API Gateway.
  • Stage is the name of the Sever like Dev, Staging or Production

Solution

  • Is your Dimensions correct? You state the name as "environment", you may want to use "stage" or maybe ApiName. When you look at the metric in the CloudWatch Console, what is the name of the dimension you want "environment"?