• This repository has been archived on 26/Oct/2023
  • Stars
    star
    531
  • Rank 83,526 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Serverless Framework plugin that creates CloudWatch alarms for functions.

Serverless AWS Alerts Plugin

NPM version Build Status Dependency Status codecov

A Serverless plugin to easily add CloudWatch alarms to functions

Installation

npm i serverless-plugin-aws-alerts --save-dev

OR

yarn add --dev serverless-plugin-aws-alerts

Usage

Basic Usage

# serverless.yml

plugins:
  - serverless-plugin-aws-alerts

custom:
  alerts:
    stages:
      - production
    topics:
      alarm:
        topic: ${self:service}-${opt:stage}-alerts-alarm
        notifications:
          - protocol: email
            endpoint: [email protected] # Change this to your email address
    alarms:
      - functionErrors
      - functionThrottles

Advanced Usage

service: your-service
provider:
  name: aws
  runtime: nodejs12.x

plugins:
  - serverless-plugin-aws-alerts
  
custom:
  alerts:
    stages: # Optionally - select which stages to deploy alarms to
      - production
      - staging

    dashboards: true

    nameTemplate: $[functionName]-$[metricName]-Alarm # Optionally - naming template for alarms, can be overwritten in definitions
    prefixTemplate: $[stackName] # Optionally - override the alarm name prefix

    topics:
      ok: ${self:service}-${opt:stage}-alerts-ok
      alarm: ${self:service}-${opt:stage}-alerts-alarm
      insufficientData: ${self:service}-${opt:stage}-alerts-insufficientData
    definitions:  # these defaults are merged with your definitions
      functionErrors:
        period: 300 # override period
      customAlarm:
        actionsEnabled: false # Indicates whether actions should be executed during any changes to the alarm state. The default is TRUE
        description: 'My custom alarm'
        namespace: 'AWS/Lambda'
        nameTemplate: $[functionName]-Duration-IMPORTANT-Alarm # Optionally - naming template for the alarms, overwrites globally defined one
        prefixTemplate: $[stackName] # Optionally - override the alarm name prefix, overwrites globally defined one
        metric: duration
        threshold: 200
        statistic: Average
        period: 300
        evaluationPeriods: 1
        datapointsToAlarm: 1
        comparisonOperator: GreaterThanOrEqualToThreshold
    alarms:
      - functionThrottles
      - functionErrors
      - functionInvocations
      - functionDuration

functions:
  foo:
    handler: foo.handler
    alarms: # merged with function alarms
      - customAlarm
      - name: fooAlarm # creates new alarm or overwrites some properties of the alarm (with the same name) from definitions
        namespace: 'AWS/Lambda'
        actionsEnabled: false
        metric: errors # define custom metrics here
        threshold: 1
        statistic: Minimum
        period: 60
        evaluationPeriods: 1
        datapointsToAlarm: 1
        comparisonOperator: GreaterThanOrEqualToThreshold

Anomaly Detection Alarms

You can create alarms using CloudWatch AnomalyDetection to alarm when data is outside a number of standard deviations of normal, rather than at a static threshold. When using anomaly detection alarms the threshold property specifies the "Anomaly Detection Threshold" seen in the AWS console.

Default alarms can also be updated to be anomaly detection alarms by adding the type: anomalyDetection property.

functions:
  foo:
    handler: foo.handler
    alarms:
      - name: fooAlarm
        type: anomalyDetection
        namespace: 'AWS/Lambda'
        metric: Invocations
        threshold: 2
        statistic: Sum
        period: 60
        evaluationPeriods: 1
        datapointsToAlarm: 1
        comparisonOperator: LessThanLowerOrGreaterThanUpperThreshold
  bar:
    handler: bar.handler
    alarms:
      - name: functionErrors
        threshold: 2
        type: anomalyDetection
        comparisonOperator: LessThanLowerOrGreaterThanUpperThreshold
      - name: functionInvocations
        threshold: 2
        type: anomalyDetection
        comparisonOperator: LessThanLowerOrGreaterThanUpperThreshold

Multiple topic definitions

You can define several topics for alarms. For example you want to have topics for critical alarms reaching your pagerduty, and different topics for noncritical alarms, which just send you emails.

In each alarm definition you have to specify which topics you want to use. In following example you get an email for each function error, pagerduty gets alarm only if there are more than 20 errors in 60s

custom:
  alerts:

    topics:
      critical:
        ok:
          topic: ${self:service}-${opt:stage}-critical-alerts-ok
          notifications:
          - protocol: https
            endpoint: https://events.pagerduty.com/integration/.../enqueue
        alarm:
          topic: ${self:service}-${opt:stage}-critical-alerts-alarm
          notifications:
          - protocol: https
            endpoint: https://events.pagerduty.com/integration/.../enqueue

      nonCritical:
        alarm:
          topic: ${self:service}-${opt:stage}-nonCritical-alerts-alarm
          notifications:
          - protocol: email
            endpoint: [email protected]

    definitions:  # these defaults are merged with your definitions
      criticalFunctionErrors:
        namespace: 'AWS/Lambda'
        metric: Errors
        threshold: 20
        statistic: Sum
        period: 60
        evaluationPeriods: 10
        comparisonOperator: GreaterThanOrEqualToThreshold
        okActions:
          - critical
        alarmActions:
          - critical
      nonCriticalFunctionErrors:
        namespace: 'AWS/Lambda'
        metric: Errors
        threshold: 1
        statistic: Sum
        period: 60
        evaluationPeriods: 10
        comparisonOperator: GreaterThanOrEqualToThreshold
        alarmActions:
          - nonCritical
    alarms:
      - criticalFunctionErrors
      - nonCriticalFunctionErrors

SNS Topics

If topic name is specified, plugin assumes that topic does not exist and will create it. To use existing topics, specify ARNs or use CloudFormation (e.g. Fn::ImportValue, Fn::Join and Ref) to refer to existing topics.

ARN support

custom:
  alerts:
    topics:
      alarm:
        topic: arn:aws:sns:${self:region}:${self::accountId}:monitoring-${opt:stage}

CloudFormation support

custom:
  alerts:
    topics:
      alarm:
        topic:
          Fn::ImportValue: ServiceMonitoring:monitoring-${opt:stage, 'dev'}
      ok:
        topic:
          Fn::Join:
            - ':'
            - - arn:aws:sns
              - Ref: AWS::Region
              - Ref: AWS::AccountId
              - example-ok-topic
      insufficientData:
        topic:
          Ref: ExampleInsufficientdataTopic

resources:
  Resources:
    ExampleInsufficientdataTopic:
      Type: AWS::SNS::Topic
      Properties:
        DisplayName: example-insufficientdata-topic
        Subscription:
          - Endpoint: [email protected]
            Protocol: EMAIL

SNS Notifications

You can configure subscriptions to your SNS topics within your serverless.yml. For each subscription, you'll need to specify a protocol and an endpoint.

The following example will send email notifications to [email protected] for all messages to the Alarm topic:

custom:
  alerts:
    topics:
      alarm:
        topic: ${self:service}-${opt:stage}-alerts-alarm
        notifications:
          - protocol: email
            endpoint: [email protected]

You can configure notifications to send to webhook URLs, to SMS devices, to other Lambda functions, and more. Check out the AWS docs here for configuration options.

Metric Log Filters

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 barExceptions. 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:
    definitions:
      barExceptions:
        metric: barExceptions
        threshold: 0
        statistic: Minimum
        period: 60
        evaluationPeriods: 1
        comparisonOperator: GreaterThanThreshold
        pattern: 'exception Bar'
      bunyanErrors:
        metric: bunyanErrors
        threshold: 0
        statistic: Sum
        period: 60
        evaluationPeriods: 1
        datapointsToAlarm: 1
        comparisonOperator: GreaterThanThreshold
        pattern: '{$.level > 40}'

Note: For custom log metrics, namespace property will automatically be set to stack name (e.g. fooservice-dev).

Custom Naming

You can define custom naming template for the alarms. nameTemplate property under alerts configures naming template for all the alarms, while placing nameTemplate under alarm definition configures (overwrites) it for that specific alarm only. Naming template provides interpolation capabilities, where supported placeholders are:

  • $[functionName] - function name (e.g. helloWorld)
  • $[functionId] - function logical id (e.g. HelloWorldLambdaFunction)
  • $[metricName] - metric name (e.g. Duration)
  • $[metricId] - metric id (e.g. BunyanErrorsHelloWorldLambdaFunction for the log based alarms, $[metricName] otherwise)

Note: All the alarm names are prefixed with stack name (e.g. fooservice-dev).

Default Definitions

The plugin provides some default definitions that you can simply drop into your application. For example:

alerts:
  alarms:
    - functionErrors
    - functionThrottles
    - 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
      treatMissingData: notBreaching # override treatMissingData

The default definitions are below.

definitions:
  functionInvocations:
    namespace: 'AWS/Lambda'
    metric: Invocations
    threshold: 100
    statistic: Sum
    period: 60
    evaluationPeriods: 1
    datapointsToAlarm: 1
    comparisonOperator: GreaterThanOrEqualToThreshold
    treatMissingData: missing
  functionErrors:
    namespace: 'AWS/Lambda'
    metric: Errors
    threshold: 1
    statistic: Sum
    period: 60
    evaluationPeriods: 1
    datapointsToAlarm: 1
    comparisonOperator: GreaterThanOrEqualToThreshold
    treatMissingData: missing
  functionDuration:
    namespace: 'AWS/Lambda'
    metric: Duration
    threshold: 500
    statistic: Average
    period: 60
    evaluationPeriods: 1
    comparisonOperator: GreaterThanOrEqualToThreshold
    treatMissingData: missing
  functionThrottles:
    namespace: 'AWS/Lambda'
    metric: Throttles
    threshold: 1
    statistic: Sum
    period: 60
    evaluationPeriods: 1
    datapointsToAlarm: 1
    comparisonOperator: GreaterThanOrEqualToThreshold
    treatMissingData: missing

Disabling default alarms for specific functions

Default alarms can be disabled on a per-function basis:

custom:
  alerts:
    alarms:
      - functionThrottles
      - functionErrors
      - functionInvocations
      - functionDuration

functions:
  bar:
    handler: bar.handler
    alarms:
      - name: functionInvocations
        enabled: false

Additional dimensions

The plugin allows users to provide custom dimensions for the alarm. Dimensions are provided in a list of key/value pairs {Name: foo, Value:bar} The plugin will always apply dimension of {Name: FunctionName, Value: ((FunctionName))}, except if the parameter omitDefaultDimension: true is passed. For example:

    alarms: # merged with function alarms
      - name: fooAlarm
        namespace: 'AWS/Lambda'
        metric: errors # define custom metrics here
        threshold: 1
        statistic: Minimum
        period: 60
        evaluationPeriods: 1
        comparisonOperator: GreaterThanThreshold
        omitDefaultDimension: true
        dimensions:
          -  Name: foo
             Value: bar
'Dimensions': [
                {
                    'Name': 'foo',
                    'Value': 'bar'
                },
            ]

Using Percentile Statistic for a Metric

Statistic not only supports SampleCount, Average, Sum, Minimum or Maximum as defined in CloudFormation here, but also percentiles. This is possible by leveraging ExtendedStatistic under the hood. This plugin will automatically choose the correct key for you. See an example below:

definitions:
  functionDuration:
    namespace: 'AWS/Lambda'
    metric: Duration
    threshold: 100
    statistic: 'p95'
    period: 60
    evaluationPeriods: 1
    datapointsToAlarm: 1
    comparisonOperator: GreaterThanThreshold
    treatMissingData: missing
    evaluateLowSampleCountPercentile: ignore

Using a Separate CloudFormation Stack

If your Serverless CloudFormation stack is growing too large and you're running out of resources, you can configure the plugin to deploy a separate stack for the CloudWatch resources. The default behaviour is to create a stack with a "-alerts" suffix in the stack name.

custom:
  alerts:
    externalStack: true

You can customize the name suffix:

custom:
  alerts:
    externalStack:
      nameSuffix: Alerts

The separate stack will be automatically deployed after you've deployed your main Serverless stack. It will also be automatically removed if you remove your main stack.

You can also enable the external stack on the command line with sls deploy --alerts-external-stack which is equivalent to adding externalStack: true to the configuration.

Dashboards

The plugin can create dashboards automatically for basic metrics.

Default setup for a single dashboard:

dashboards: true

Create a vertical dashboard:

dashboards: vertical

Create dashboards only in specified stages:

dashboards:
  stages:
    - production
    - staging
  templates:
    - default

License

MIT Β© A Cloud Guru

More Repositories

1

intro-to-CloudFormation_AC

An Introduction to CloudFormation taking students through constructing templates and the use of advanced functionality
193
star
2

gcp-cloud-engineer

Lab files for A Cloud Guru's Google Certified Associate Cloud Engineer course -- ACG GCP ACE
Shell
185
star
3

AdvancedCloudFormation

Python
137
star
4

serverless-workshop

JavaScript
137
star
5

DynamoDB-B2P

Lab files for A Cloud Guru 'Amazon DynamoDB - From Beginner to Pro '
Python
76
star
6

alexacourse

JavaScript
66
star
7

automating-aws-with-python

Repository for Automating AWS with Python course
Python
49
star
8

dynamodb

PHP
27
star
9

serverless-framework-video-example

A serverless video sharing application example using Serverless Framework
JavaScript
21
star
10

s3

Files for the PHP and S3 lab in the Certified Developer - Associate course
PHP
18
star
11

devops

Repository for the DevOps DC01 course
HTML
18
star
12

generator-serverless-service

JavaScript
16
star
13

oprah

Package to deploy parameters to AWS
JavaScript
14
star
14

content-kcna

Will Boyd, 1124, Kubernetes and Cloud Native Associate (KCNA)
14
star
15

serverless-plugin-dynamo-autoscaling

Serverless Plugin for Amazon DynamoDB Auto Scaling using cloudformation.
JavaScript
14
star
16

todoer-demo

JavaScript
14
star
17

acloudguru

Shell
13
star
18

serverless-plugin-package-dotenv-file

A Serverless plugin to copy a .env file into the serverless package
JavaScript
13
star
19

serverless-plugin-cloudwatch-sumologic

Plugin which auto-subscribes a log delivery lambda function to lambda log groups created by serverless
JavaScript
12
star
20

Your_Very_Own_API_BT

A Cloud Guru Maker Lab on API construction
JavaScript
10
star
21

hands-on-aws-troubleshooting

Faye Ellis, Hands-on AWS Troubleshooting (1127)
HTML
10
star
22

Does-Twitter-Hate-Cats

JavaScript
9
star
23

serverless-transceiver

JavaScript
8
star
24

api-gateway-policy-gen

A Policy Generator for API Gateway
JavaScript
8
star
25

EventDrivenSecurity

Python
8
star
26

serverless-plugin-crypt

JavaScript
7
star
27

makerlab-103-Polly

Resource files for the Polly Maker lab #103
Python
6
star
28

advanced-terraform-with-azure

1118, Jess Hoch, Advanced Terraform with Azure, February 2022
HCL
6
star
29

content-designing-applications-for-kubernetes

1082 - Designing Applications for Kubernetes, Will Boyd
JavaScript
6
star
30

AlexaDevs

JavaScript
5
star
31

BringInTheWashing

Maker Lab exploring image rekognition
JavaScript
4
star
32

Let_the_Free_Times_Roll_BT

An A Cloud Guru Maker Lab - Let the Free Time Roll - Using AWS Continuous Deployment tools.
TypeScript
4
star
33

AcloudguruOpsworks

PHP
4
star
34

metadata

PHP
4
star
35

hands-on-troubleshooting-with-terraform

1099, Hands-On Troubleshooting with Terraform, Jess Hoch
4
star
36

makerlab-securing-s3

Makerlab Securing S3
HTML
4
star
37

find-close-meteorites

A demo project that uses Python and NASA data to find meteor landing sites
Python
4
star
38

metadata-env-buildkite-plugin

Buildkite plugin to inject buildkite-agent metadata into environment
Shell
4
star
39

elastic-cache-challenge

A Cloud Guru Challenge, David Thomas, June 2021
Python
4
star
40

AdvancedNetworking

3
star
41

blog-acg-graphql-evolution

JavaScript
3
star
42

ctaws-plant-shop

Will Boyd
Shell
2
star
43

memealator-9000

Memealator 9000 uses AWS lambda to generate memes
Python
2
star
44

Advanced_Alexa_Skills_lab

Lab files for A Cloud Guru 'Advanced Alexa Skills Kit'
JavaScript
2
star
45

content-AI-900

1080, AI-900: Microsoft Azure AI Fundamentals, Clint Bonnett, July 27 2021
Python
2
star
46

prelogger

Logger which supports prefixing log messages with metadata such as correlation IDs
JavaScript
1
star
47

misc-serverless-workshop-videos

Demo videos for the serverless for beginners course and the serverless workshop
1
star
48

content-elastic-certified-engineer

1086, Elastic Certified Engineer, Myles Young, July 1 2021
Shell
1
star
49

Lab-Services---Cloud-Engineer-Testing

Repository for Lab Services Cloud Engineer interview testing
Shell
1
star
50

Cloud_Sandbox_Examples

1
star