1.0.0 • Published 17 days ago

serverless-aws-alarms v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
17 days ago

Serverless AWS Alarms Plugin

serverless npm node serverless license

Serverless framework plugin that easily creates CloudWatch alarms for lambdas.

Usage

service: service-name

plugins:
  - serverless-aws-alarms

custom:
  awsAlarms:
    stages: # optional - select which stages to deploy alarms to
      - staging
      - production

    actions:
      default: # optional - default actions for all alarms
        ok: arn:aws:sns:${self:provider.region}:${aws:accountId}:my-team-alerts-ok
        alarm: arn:aws:sns:${self:provider.region}:${aws:accountId}:my-team-alerts-alarm
        insufficientData: arn:aws:sns:${self:provider.region}:${aws:accountId}:my-team-alerts-insufficient-data
      critical: # optional - create more actions
        ok: !Ref MyTeamCriticalAlertsTopic # you can also use !Ref and other CloudFormation functions
        alarm: !ImportValue 'my-team-alerts'

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

    definitions: # these defaults are merged with your definitions
      lambdaErrors:
        enabled: true
        period: 300 # override period
      lambdaTimeout:
        enabled: true
        period: 300 # override period
        threshold: 2000 # override threshold
        okActions:
          - critical
        alarmActions:
          - critical
        insufficientDataActions: [] # override insufficientDataActions to empty array (instead of using default)
      customAlarm: # create new alarm
        enabled: false
        prefixTemplate: $[stackName] # Optional - override the alarm name prefix
        nameTemplate: $[functionName]-high-duration-alarm # Optional - override the alarm name
        description: 'My custom alarm'
        namespace: 'AWS/Lambda'
        metric: Duration
        threshold: 200
        statistic: Average
        period: 300
        evaluationPeriods: 1
        datapointsToAlarm: 1
        comparisonOperator: GreaterThanOrEqualToThreshold

provider:
  name: aws
  runtime: nodejs18x

functions:
  foo:
    name: ${self:service}-foo
    handler: foo.handler
    alarms: # merged with definitions
      customAlarm:
        enabled: true
      someAlarm: # creates new alarm for this lambda or overwrite some properties of the alarm (with the same name) from definitions
        namespace: 'AWS/Lambda'
        metric: Errors
        threshold: 1
        statistic: Minimum
        period: 60
        evaluationPeriods: 1
        datapointsToAlarm: 1
        comparisonOperator: GreaterThanOrEqualToThreshold
        actionsEnabled: false

Alarm defenition

Detailed description of the properties can be found in the AWS documentation.

namedescriptiontypedefault
enabledIndicates whether the alarm will be created or notbooleanfalse
prefixTemplatePrefix alarm namestring''
nameTemplateThe name of the alarmstring$[lambdaName]-$[definitionName]
descriptionThe description of the alarmstring
namespaceThe namespace of the metric associated with the alarmstring
metricThe name of the metric associated with the alarmstring
comparisonOperatorThe arithmetic operation to use when comparing the specified statistic and thresholdstring
statisticThe statistic for the metric associated with the alarmstring
thresholdThe value to compare with the specified statisticnumber
periodThe period, in seconds, over which the statistic is appliednumber
evaluationPeriodsThe number of periods over which data is compared to the specified thresholdnumber
datapointsToAlarmThe number of datapoints that must be breaching to trigger the alarmnumber
actionsEnabledIndicates whether actions should be executed during any changes to the alarm stateboolean
okActionsThe actions to execute when this alarm transitions to the OK state from any other statestring[]
alarmActionsThe list of actions to execute when this alarm transitions into an ALARM state from any other statestring[]
insufficientDataActionsThe actions to execute when this alarm transitions to the INSUFFICIENT_DATA state from any other statestring[]
treatMissingDataSets how this alarm is to handle missing data pointsstring
metricFilterCreate an alarm based on a pattern found in a log groupAlarmMetricFilterDefinition

Metric filter definition

Detailed description of the properties can be found in the AWS documentation.

namedescriptiontypedefault
patternA filter pattern for extracting metric data out of ingested log events. For more information, see Filter and Pattern Syntaxstring required
metricValueThe value that is published to the CloudWatch metricstring | number
defaultValueThe value to emit when a filter pattern does not match a log eventnumber
nameTemplateThe name of the metric filterstring$[lambdaName]$[definitionName]AlarmFilter

Name template placeholders

You can use the following placeholders in the nameTemplate and prefixTemplate properties:

  • stackName - the name of the stack
  • lambdaName - the name of the lambda
  • lambdaId - the name of the lambda resource (the key in functions section)
  • lambdaLogicalId - the name of the lambda resource in the CloudFormation template
  • metricName - the name of the metric (from alarm defenition)
  • definitionName - the name of the alarm definition

Usage example:

nameTemplate: $[stackName]-$[lambdaName]-$[metricName]-alarm
prefixTemplate: $[stackName]