AWS LambdaでSentryを使う

公開日時

AWS Lambdaで動くJSアプリケーションのエラー検知をしようと思い、Sentryを導入した。

しかし、[[Sentry.captureException]]を使って手動でエラー通知を行ったところ、うまくエラー通知が送信されない現象が発生した。

issueにも同様の現象が挙がっており、コメントにあった[[Sentry.flush]]を使うことでエラーが通知されるようになった。

[[@sentry/node] AWS Lambda and other Serverless solutions support · Issue #1449](https://github.com/getsentry/sentry-javascript/issues/1449)

import * as Sentry from '@sentry/node';

Sentry.init({ dsn: process.env.SENTRY_URL! });

const handler = async (event, context) => {
  try {
    someFunction();
  } catch (error) {
    Sentry.captureException(error);
    await Sentry.flush(2500);
  }
  
  return context.succeed({
    200,
    body: JSON.stringify({
      message: 'ok'
    })
  });
};

export { handler };

Related #aws

AWSのコスト異常検出を設定する

意図しない課金を防ぐためにとりあえず設定しておくと良さそう

Nodejs12のLambdaでawscliを使う

aws-lambda-layer-awscliを使う

CDK aws-lambda-nodejsのビルド時間を短縮する

Parcel v2.0.0-beta.1を使ってローカルでバンドルする

CloudWatch Eventsで日本時刻(JST)の月初に実行したい

Lオプションの存在を知った