AWS Lambda

Bugsnag can automatically report unhandled errors and capture useful metadata in AWS Lambda functions.

This documentation is for version 7 of the Bugsnag JavaScript notifier. If you are using older versions, we recommend upgrading to the latest release using our Upgrade guide. Documentation for the previous release can be found on our legacy pages.

Installation

Install and configure @bugsnag/js, then install the AWS Lambda plugin using npm or yarn:

yarn add @bugsnag/plugin-aws-lambda
# or
npm install --save @bugsnag/plugin-aws-lambda

Usage

To start Bugsnag with the AWS Lambda integration, pass the plugin to Bugsnag.start:

const Bugsnag = require('@bugsnag/js')
const BugsnagPluginAwsLambda = require('@bugsnag/plugin-aws-lambda')

Bugsnag.start({
  plugins: [BugsnagPluginAwsLambda],
})

Start handling errors in your Lambda function by wrapping your handler with Bugsnag’s handler:

const bugsnagHandler = Bugsnag.getPlugin('awsLambda').createHandler()

const handler = async (event, context) => {
  return {
    statusCode: 200,
    body: JSON.stringify({ message: 'Hello, World!' })
  }
}

module.exports.lambdaHandler = bugsnagHandler(handler)

Bugsnag’s createHandler supports both async and callback handlers.

Automatically captured data

Bugsnag will automatically capture the Lambda function’s context in the “AWS Lambda Context” tab on every error.

You can remove individual items from context using redactedKeys:

Bugsnag.start({
  redactedKeys: ['invokedFunctionArn']
})

You can also remove the tab entirely:

Bugsnag.clearMetadata('AWS Lambda context')

Session tracking

A session will be reported automatically each time your Lambda function is called.

This behavior can be disabled using the autoTrackSessions configuration option.

Configuration

The Bugsnag AWS Lambda plugin can be configured by passing options to createHandler.

flushTimeoutMs

Bugsnag will wait for events and sessions to be delivered before allowing the Lambda function to exit. This option can be used to control the maximum amount of time to wait before timing out.

By default, Bugsnag will timeout after 2000 milliseconds.

const bugsnagHandler = Bugsnag.getPlugin('awsLambda').createHandler({
  flushTimeoutMs: 5000
})

If a timeout does occur, Bugsnag will log a warning and events & sessions may not be delivered.

lambdaTimeoutNotifyMs

Bugsnag will automatically notify just before the Lambda function times out. This option can be used to control the number of milliseconds before a timeout that Bugsnag should be notified.

By default, Bugsnag will notify 1000 milliseconds before the Lambda timeout.

const bugsnagHandler = Bugsnag.getPlugin('awsLambda').createHandler({
  lambdaTimeoutNotifyMs: 2000
})

Set this value to 0 to disable the automatic notification.

If this value is too low, Bugsnag may not be notified of timeouts because the Lambda function could be stopped before the notification is delivered.