A Serverless plugin to easily add CloudWatch alarms to functions
npm i serverless-plugin-aws-alerts
service: your-service
provider:
name: aws
runtime: nodejs4.3
custom:
notifications:
- protocol: email
endpoint: john@acloud.guru
codeRedNotifications:
- protocol: sms
endpoint: +61407874400
- protocol: email
endpoint: xxxx@pageup.slack.com
alerts:
stages: # Optionally - select which stages to deploy alarms to
- producton
- staging
topics:
ok: ${self:service}-${opt:stage}-alerts-ok
insufficientData: ${self:service}-${opt:stage}-alerts-insufficientData
alarm:
topic: ${self:service}-${opt:stage, self:provider.stage}-alerts-alarm
notifications: ${self:custom.notifications}
codeRedAlarm:
topic: ${self:service}-${opt:stage, self:provider.stage}-alerts-alarm-code-red
notifications: ${self:custom.codeRedNotifications}
definitions: # these defaults are merged with your definitions
functionErrors:
period: 300 # override period
customAlarm:
namespace: 'AWS/Lambda'
metric: duration
threshold: 200
statistic: Average
period: 300
evaluationPeriods: 1
comparisonOperator: GreaterThanThreshold
functionCodeRed: #Something is _really_ going wrong and the team needs to know about it NOW!
namespace: 'AWS/Lambda'
metric: Errors
threshold: 20
statistic: Sum
period: 300
evaluationPeriods: 1
comparisonOperator: GreaterThanOrEqualToThreshold
topics:
alarm: codeRedAlarm
global:
- functionCodeRed
- functionThrottles
- functionErrors
function:
- functionInvocations
- functionDuration
plugins:
- serverless-plugin-aws-alerts
functions:
foo:
handler: foo.handler
alarms: # merged with function alarms
- customAlarm
- name: fooAlarm
namespace: 'AWS/Lambda'
metric: errors # define custom metrics here
threshold: 1
statistic: Minimum
period: 60
evaluationPeriods: 1
comparisonOperator: GreaterThanThreshold
If topic name is specified, plugin assumes that topic does not exist and will create it. To use existing topics, specify ARNs instead.
You can monitor a log group for a function for a specific pattern. Do this by adding the pattern key. You can learn about custom patterns at: http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html
The following would create a custom metric log filter based alarm named barAlarm
. Any function that included this alarm would have its logs scanned for the pattern exception Bar
and if found would trigger an alarm.
custom:
alerts:
function:
- name: barAlarm
metric: barExceptions
threshold: 0
statistic: Minimum
period: 60
evaluationPeriods: 1
comparisonOperator: GreaterThanThreshold
pattern: 'exception Bar'
- name: bunyanErrors
metric: BunyanErrors
threshold: 0
statistic: Sum
period: 60
evaluationPeriods: 1
comparisonOperator: GreaterThanThreshold
pattern: '{$.level > 40}'
Note: For custom log metrics, namespace property will automatically be set to stack name (e.g.
fooservice-dev
).
The plugin provides some default definitions that you can simply drop into your application. For example:
alerts:
global:
- functionThrottles
- functionErrors
function:
- functionInvocations
- functionDuration
If these definitions do not quite suit i.e. the threshold is too high, you can override a setting without creating a completely new definition.
alerts:
definitions: # these defaults are merged with your definitions
functionErrors:
period: 300 # override period
The default definitions are below.
definitions:
functionInvocations:
namespace: 'AWS/Lambda'
metric: Invocations
threshold: 100
statistic: Sum
period: 60
evaluationPeriods: 1
comparisonOperator: GreaterThanThreshold
functionErrors:
namespace: 'AWS/Lambda'
metric: Errors
threshold: 10
statistic: Maximum
period: 60
evaluationPeriods: 1
comparisonOperator: GreaterThanThreshold
functionDuration:
namespace: 'AWS/Lambda'
metric: Duration
threshold: 500
statistic: Maximum
period: 60
evaluationPeriods: 1
comparisonOperator: GreaterThanThreshold
functionThrottles:
namespace: 'AWS/Lambda'
metric: Throttles
threshold: 50
statistic: Sum
period: 60
evaluationPeriods: 1
comparisonOperator: GreaterThanThreshold
MIT © A Cloud Guru