I would like to implement following in Serverless framework.
I have multiple services and each serverless service has multiple lambda functions. I would like to trigger the CloudWatch alarm by all Lambda functions that fails or throw exception.
Here is the code to be triggered by one lambda function:
resources:
Resources:
TopicAlarm4xx:
Type: AWS::SNS::Topic
Properties:
TopicName: error_notification_topic
LambdaAlarm4xx:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: 4xx errors detected at Lambda
Namespace: AWS/Lambda
MetricName: 4XXErrors
Statistic: Sum
Threshold: 1
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
Period: 300
Dimensions:
- Name: LambdaName
Value: # Value: !Ref LambdaName
Ref: functionName
Here is how it works for one lambda function
service: TestService
plugins:
- serverless-pseudo-parameters
custom:
stage: dev
log_notification: "log_notification_${self:custom.stage}"
provider:
iamRoleStatements:
- Effect: "Allow"
Action:
- logs:*
Resource: "arn:aws:logs:*:*:*"
- Effect: 'Allow'
Action:
- "sns:*"
Resource: "*"
functions:
logNotification:
handler: handler.logNotification
events:
- sns: arn:aws:sns:#{AWS::Region}:#{AWS::AccountId}:${self:custom.log_notification}
testError:
handler: handler.testError
events:
- http:
path: testerror
method: get
cors: true
resources:
Resources:
TopicAlarmLambdaError:
Type: AWS::SNS::Topic
Properties:
TopicName: ${self:custom.log_notification}
LambdaAlarmForErrors:
Type: AWS::CloudWatch::Alarm
Properties:
AlarmDescription: Lambda errors detected in All Lambdas
Namespace: AWS/Lambda
MetricName: Errors
Statistic: Sum
Threshold: 1
ComparisonOperator: GreaterThanOrEqualToThreshold
EvaluationPeriods: 1
Period: 30 # seconds
AlarmActions:
- Ref: TopicAlarmLambdaError
Dimensions:
- Name: FunctionName
Value: ${self:service}-${self:custom.stage}-testError