# Lambda Layer - CJS | Sentry for AWS Lambda

The easiest way to get started with Sentry is to use the Sentry [Lambda Layer](https://docs.aws.amazon.com/lambda/latest/dg/adding-layers.html) instead of adding `@sentry/aws-serverless` with `npm` or `yarn` [manually](https://docs.sentry.io/platforms/javascript/guides/aws-lambda/install/cjs-npm.md). 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](https://docs.sentry.io/platforms/javascript/guides/aws-lambda/install/cjs-layer.md#alternative-initialize-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](https://docs.sentry.io/platforms/javascript/guides/aws-lambda/install/esm-npm.md).

## [1. Prerequisites](https://docs.sentry.io/platforms/javascript/guides/aws-lambda/install/cjs-layer.md#1-prerequisites)

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.

## [2. Add the Sentry Lambda Layer](https://docs.sentry.io/platforms/javascript/guides/aws-lambda/install/cjs-layer.md#2-add-the-sentry-lambda-layer)

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

[![](https://docs.sentry.io/_next/image/?url=%2Fmdx-images%2Flambda_view-4T3IE353.png%232028x1016\&w=3840\&q=75)](https://docs.sentry.io/mdx-images/lambda_view-4T3IE353.png)

**Specify an ARN** tab as illustrated:

[![](https://docs.sentry.io/_next/image/?url=%2Fmdx-images%2Fadd_layer-SLRFQLDC.png%231688x836\&w=3840\&q=75)](https://docs.sentry.io/mdx-images/add_layer-SLRFQLDC.png)

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

Select Region



## [3. Initialize the SDK with Environment Variables](https://docs.sentry.io/platforms/javascript/guides/aws-lambda/install/cjs-layer.md#3-initialize-the-sdk-with-environment-variables)

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.

In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](https://docs.sentry.io/concepts/key-terms/tracing.md).

Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.

Error Monitoring\[ ]Tracing

Set the following environment variables in your Lambda function configuration:

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

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

[![](https://docs.sentry.io/_next/image/?url=%2Fmdx-images%2Fcjs_env_vars-QJAWKKW4.png%231806x764\&w=3840\&q=75)](https://docs.sentry.io/mdx-images/cjs_env_vars-QJAWKKW4.png)

## [Alternative: Initialize the SDK in Code](https://docs.sentry.io/platforms/javascript/guides/aws-lambda/install/cjs-layer.md#alternative-initialize-the-sdk-in-code)

Instead of [Step 3, setting environment variables](https://docs.sentry.io/platforms/javascript/guides/aws-lambda/install/cjs-layer.md#3-initialize-the-sdk-with-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](https://docs.sentry.io/platforms/javascript/guides/aws-lambda/install/cjs-layer.md#1-prerequisites) and [step 2](https://docs.sentry.io/platforms/javascript/guides/aws-lambda/install/cjs-layer.md#2-add-the-sentry-lambda-layer) before proceeding.

`index.js`

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

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

  // Adds request headers and IP for users, for more info visit:
  // https://docs.sentry.io/platforms/javascript/guides/aws-lambda/configuration/options/#sendDefaultPii
  sendDefaultPii: true,
  //  performance

  // 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
  // Learn more at
  // https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
  tracesSampleRate: 1.0,
  //  performance
});

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!
