Serverless Plugin AWS Alertsとは何か?:Serverlessアプリケーションの監視を自動化するプラグイン

Serverless Plugin AWS Alertsとは何か?:Serverlessアプリケーションの監視を自動化するプラグイン

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは!

今回は、Serverless FrameworkのプラグインであるServerless Plugin AWS Alertsについてご紹介します。Serverless Plugin AWS Alertsは、Serverlessアプリケーションの監視を自動化するプラグインです。

はじめに

Serverless Plugin AWS Alertsは、Serverless Frameworkを使用して開発されたアプリケーションを監視するためのプラグインです。AWS LambdaやAPI Gatewayなどのサーバーレスサービスを使用して開発されたアプリケーションを監視するためのツールとして、Serverless Plugin AWS Alertsは非常に有用です。

Serverless Plugin AWS Alertsを使用することで、サーバーレスアプリケーションを監視し、異常があった場合に警告を受け取ることができます。また、Serverless Plugin AWS Alertsを使用することで、開発者はサーバーレスアプリケーションの状態をリアルタイムで監視することができます。

Serverless Plugin AWS Alertsのインストール

Serverless Plugin AWS Alertsをインストールするには、以下のコマンドを実行します。

npm install serverless-plugin-aws-alerts

Serverless Plugin AWS Alertsをインストールしたら、サーバーレスアプリケーションのプロジェクトのserverless.ymlファイルに以下のプラグインを追加します。

plugins:
  - serverless-plugin-aws-alerts

Serverless Plugin AWS Alertsの設定

Serverless Plugin AWS Alertsを使用するには、サーバーレスアプリケーションのプロジェクトのserverless.ymlファイルに以下の設定を追加します。

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

上記の設定を行うことで、サーバーレスアプリケーションの異常を検知し、SNSトピックに警告を送信することができます。

本プラグインには、その他運用監視に便利な様々な設定が可能となっているので、気になる方は公式ページを確認してください!

便利なテクニックを紹介

本プラグインはSNSトピックへメッセージを作成してくれますが、SNSから先の送信までは自動的にしてくれません。(Emailであれば可能)そのため、Slack等へ通知するには、通常SNSからLambda等を実行し別途通知処理を実装することが必要です。

ここで便利なのが、AWS Chatbotです。

AWS Chatbot は、Slack チャンネルで AWS 用の ChatOps を簡単に設定することが可能なインタラクティブエージェントです。

AWS Chatbotを使用すれば、AWSの障害状況をいち早くSlackチャットで検知可能となります。

まとめ

今回は、Serverless Plugin AWS Alertsについてご紹介しました。Serverless Plugin AWS Alertsを使用することで、サーバーレスアプリケーションの監視を自動化することができます。また、AWS Chatbotを組み合わせることでより効率的な運用が可能となります。

サーバーレス開発低コストなAWS開発内製化はお気軽にお問い合わせください。