Lambda Layer - CJS

Learn how to add the Sentry Node Lambda Layer to use Sentry in your Lambda functions running in CommonJS (CJS)

The easiest way to get started with Sentry is to use the Sentry Lambda Layer instead of adding @sentry/aws-serverless with npm or yarn manually. If you follow this guide, you don't have to worry about deploying Sentry dependencies alongside your function code. To actually start the SDK, you can decide between setting up the SDK using environment variables or in your Lambda function code. We recommend using environment variables as it's the easiest way to get started. Initializing the SDK in code instead of setting environment variables gives you more control over the SDK setup if you need it.

This installation method does not work with Lambda functions running in EcmaScript Modules (ESM) mode, using import syntax. If you're running your function in ESM, follow the ESM guide.

Before you begin, make sure you have the following:

  • You have a Lambda function that is running in CommonJS (CJS) mode, using require syntax.
  • You know the AWS region that your function is deployed to.

Add the Sentry Layer by navigating to your Lambda function. Select Layers, then Add a Layer.

Specify an ARN tab as illustrated:

Finally, set the region and copy the provided ARN value into the input.

Select Region

The easiest way to set up the SDK is to start and configure it using environment variables. This way, you don't have to modify your Lambda function code.

Set the following environment variables in your Lambda function configuration:

Copied
NODE_OPTIONS="-r @sentry/aws-serverless/awslambda-auto"
SENTRY_DSN="https://examplePublicKey@o0.ingest.sentry.io/0"
SENTRY_TRACES_SAMPLE_RATE="1.0"

To set environment variables, navigate to your Lambda function, select Configuration, then Environment variables:

Instead of Step 3, setting environment variables, you can also manually initialize the SDK in your Lambda function code. This way, you can customize the SDK setup further. Note that you don't have to actually install an NPM package for this to work, as the package is already included in the Lambda Layer.

Make sure you completed step 1 and step 2 before proceeding.

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

Sentry.init({
  dsn: "__PUBLIC_DSN__",
  // 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
});

It's important to add both, the Sentry.init call outside the handler function and the Sentry.wrapHandler wrapper around your function to automatically catch errors and performance data.

That's it - you're all set!

The instructions above are written for SDK version 8 (the most recent version). You can also install a v7 version of the Sentry Lambda layer in case you can't upgrade to v8. The procedure is identical to the instructions above except for two differences:

The v7 Lambda layer has a different ARN:

Copied
arn:aws:Lambda:us-west-1:943013980633:layer:SentryNodeServerlessSDKv7:3

Modify and copy the ARN value for your region into the input, e.g. for region :us-west-1 and the current v7 Lambda layer version :3:

The @sentry/aws-serverless package was called @sentry/serverless prior to version 8. Therefore, for the v7 layer, adjust your NODE_OPTIONS environment variable:

Copied
NODE_OPTIONS="-r @sentry/serverless/dist/awslambda-auto"

The other environment variables remain the same as above.

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").