AWS Lambda

Learn about Sentry's integration with AWS Lambda.

Looking for instructions on how to add Sentry without modifying your code? Check out these docs instead.

Before you begin, note:

  • The estimated time to configure the integration is 5 - 15 minutes.
  • You need to have experience with AWS console and a basic understanding of Lambda.
  • You'll need an AWS account and you have to create an IAM user.
This guide is for version 8.0.0 and up of @sentry/aws-serverless.

Create a deployment package on your local machine and install the required dependencies in the deployment package. For more information, see Building an AWS Lambda deployment package for Node.js.

Copied
npm install @sentry/aws-serverless @sentry/profiling-node --save

We also support installing Sentry in Lambda Layer.

You can use the AWS Lambda integration for the Node like this:

Copied
const Sentry = require("@sentry/aws-serverless");

Sentry.init({
  dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",

  // Add Tracing by setting tracesSampleRate and adding integration
  // Set tracesSampleRate to 1.0 to capture 100% of transactions
  // We recommend adjusting this value in production
  tracesSampleRate: 1.0,
});

exports.handler = Sentry.wrapHandler(async (event, context) => {
  // Your handler code
});

Sentry reports timeout warning when the function is within 500ms of its execution time. You can turn off timeout warnings by setting captureTimeoutWarning to false in the handler options. To change timeout warning limit, assign a numeric value (in ms) to timeoutWarningLimit

Copied
exports.handler = Sentry.wrapHandler(yourHandler, {
  captureTimeoutWarning: false,
});

By default, Sentry captures errors raised by your handler. However, your handler might return a Promise.allSettled result. In this case, even if one of the messages has failed, Sentry won't capture any exception.

The captureAllSettledReasons (default: false) option captures all promise rejected results

Copied
exports.handler = Sentry.wrapHandler(
  () => {
    return Promise.allSettled([
      Promise.rejected(new Error("first")),
      Promise.rejected(new Error("second")),
    ]);
  },
  { captureAllSettledReasons: true },
);
// `first` and `second` errors are captured

With the AWS Lambda integration enabled, the Node SDK will:

  • Automatically report all events from your Lambda Functions.
  • Allows you to modify the transaction sample rate using tracesSampleRate.
  • Issue reports automatically include:
    • A link to the cloudwatch logs
    • Function details
    • sys.argv for the function
    • AWS Request ID
    • Function execution time
    • Function version
  • Sentry holds the thread for up to 2 seconds to report errors. You can change flush time limit by defining a flushTimeout value in the handler options
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").