当前位置: 首页 > 软件库 > 云计算 > Serverless 系统 >

serverless-plugin-aws-alerts

授权协议 MIT License
开发语言 JavaScript
所属分类 云计算、 Serverless 系统
软件类型 开源软件
地区 不详
投 递 者 赫连飞沉
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Serverless AWS Alerts Plugin

codecov

A Serverless plugin to easily add CloudWatch alarms to functions

Installation

npm i serverless-plugin-aws-alerts

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: name@domain.com # 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 alarmsreaching 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 exampleyou get an email for each function error, pagerduty gets alarm only if there are more than 20errors 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: alarms@email.com

    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: me@example.com
            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 me@example.com for all messages to the Alarm topic:

custom:
  alerts:
    topics:
      alarm:
        topic: ${self:service}-${opt:stage}-alerts-alarm
        notifications:
          - protocol: email
            endpoint: me@example.com

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 withoutcreating 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 defaultbehaviour 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 Serverlessstack. 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-stackwhich 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

  • 问题描述: 客户那里poc,填写aws的AK(access key) SK(secret key)之后,链接不上到aws中国区。报这个错,很显然是网络问题,检查发现是服务器DNS没有设置,无法解析。 链接不上aws还可能的情况是,服务器时间或者时区不对,也是可能连不上的。 解决方案: 对于dns和时间的问题当然是对症下药了,直接设置一下就好了 再有就是,客户服务器根本不让访问外网的情况,就可以给a

  • 今天Amazon Web Service发布了Elasticsearch的开源分发包,项目名为Open Distribution for Elasticsearch。其中包括了Security,Alerting,SQL,performance工具等一组套件。因为有幸参与了其中SQL插件的开发,所以在这里打一打广告,欢迎大家Fork! 官方介绍:https://opendistro.github.i

  • 亚马逊AWS的Serverless架构 作者:chszs,未经博主允许不得转载。经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs Serverless平台允许运行应用程序,包括计算、存储和网络——无需启动和管理单个(虚拟)机器。本文主要介绍AWS上的Serverless架构,包括Lambda、API Gateway、DynamoDB、S3等。Serverles

  • 操作系统为Oracle Linux 7.6 安装python sudo yum install python 安装 pip sudo yum install python-setuptools sudo yum install python-pip python-wheel 升级pip sudo pip install --upgrade pip 安装aws cli sudo pip ins

  • 1.安装EPEL源 sudo yum install epel-release 2.安装PIP工具 sudo yum install python-pip 3.鉴于网络原因,修改pip安装源 linux下运行命令(没有则创建) mkdir -p ~/.pip vi ~/.pip/pip.conf 然后写入如下内容并保存 [global] trusted-host = mirrors.aliyun.

  • 一、报错详情 Traceback (most recent call last): File "/usr/local/bin/aws", line 27, in <module> sys.exit(main()) File "/usr/local/bin/aws", line 23, in main return awscli.clidriver.main() File

  • 最近也用了aws lambda一段时间了。学习到了一些东西,这里整理一些。 lambda是采用函数的方式进行调用,背后不需要服务器,需要多少资源进行申请调整即可。但是如果只有lambda函数,就好比你写了一个python函数一样,外界不可调用,这个时候需要把lambda和api gateway给连接起来,这样你就可以通过url传递参数的方式来调用lambda函数,当然你可以通过lambda函数来进

  • 解决方案: 1. sudo aws install awscli 2.如果1 不成功后,直接执行以下步骤: sudo  python -m pip install --upgrade pip sudo pip uninstall awscli sudo pip install awscli

  • Getting Started Reference Documentation For further reference, please consider the following sections: Official Apache Maven documentation Spring Boot Maven Plugin Reference Guide Create an OCI image

  • 使用AWS CLI在S3上创建了一个bucket,上传文件的时候报以下错误: A client error (AccessDenied) occurred when calling the CreateMultipartUpload operation: Anonymous users cannot initiate multipart uploads. Please authenticate.

 相关资料
  • serverless-plugin-typescript Originally developed by Prisma Labs, now maintained in scope of Serverless, Inc Serverless plugin for zero-config Typescript support Features Zero-config: Works out of the

  • Deploy AppSync API's in minutes using this Serverless plugin. Getting Started Be sure to check out all that AWS AppSync has to offer. Here are a few resources to help you understand everything needed

  • Serverless WarmUp Plugin ♨ Keep your lambdas warm during winter. Requirements: Serverless v1.12.x or higher (Recommended v1.33.x or higher because of this). AWS provider How it works WarmUp solves col

  • Serverless Optimize Plugin Bundle with Browserify, transpile and minify with Babel automatically to your NodeJS runtime compatible JavaScript. This plugin is a child of the great serverless-optimizer-

  • Serverless Plugin Canary Deployments A Serverless plugin to implement canary deployments of Lambda functions, making use of the traffic shifting feature in combination with AWS CodeDeploy Contents Ins

  • aws-serverless-koa This library enables you to utilize AWS Lambda and Amazon API Gateway to respond to web and API requests using the Koa.js application framework Installation $ npm install --save aws