|
| 1 | +--- |
| 2 | +title: Lambda Layer - CJS |
| 3 | +description: "Learn how to add the Sentry Node Lambda Layer to use Sentry in your Lambda functions running in CommonJS (CJS)" |
| 4 | +sidebar_order: 1 |
| 5 | +noindex: true |
| 6 | +--- |
| 7 | + |
| 8 | +The easiest way to get started with Sentry is to use the Sentry [Lambda Layer](https://docs.aws.amazon.com/Lambda/latest/dg/configuration-layers.html) instead of adding `@sentry/aws-serverless` with `npm` or `yarn` [manually](../cjs-npm). |
| 9 | +If you follow this guide, you don't have to worry about deploying Sentry dependencies alongside your function code. |
| 10 | +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](#alternative-initialize-the-sdk-in-code) instead of setting environment variables gives you more control over the SDK setup if you need it. |
| 11 | + |
| 12 | +<Alert> |
| 13 | + |
| 14 | +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](../esm-npm). |
| 15 | + |
| 16 | +</Alert> |
| 17 | + |
| 18 | +## 1. Prerequisites |
| 19 | + |
| 20 | +Before you begin, make sure you have the following: |
| 21 | + |
| 22 | +- You have a Lambda function that is running in CommonJS (CJS) mode, using `require` syntax. |
| 23 | +- You know the AWS region that your function is deployed to. |
| 24 | + |
| 25 | +## 2. Add the Sentry Lambda Layer |
| 26 | + |
| 27 | +Add the Sentry Layer by navigating to your Lambda function. Select **Layers**, then **Add a Layer**. |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +**Specify an ARN** tab as illustrated: |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +Modify and copy the ARN value for your region into the input, e.g. for region `us-west-1` and the current v8 Lambda layer version `7`: |
| 36 | + |
| 37 | +``` |
| 38 | +arn:aws:Lambda:us-west-1:943013980633:layer:SentryNodeServerlessSDKv8:7 |
| 39 | +``` |
| 40 | + |
| 41 | +<Alert> |
| 42 | + |
| 43 | + Previously, the v8 Lambda layer was published under the `SentryNodeServerlessSDK` name. |
| 44 | + This target will not receive updates anymore. |
| 45 | + |
| 46 | + If you need to continue using v8 please use the `SentryNodeServerlessSDKv8` layer as described above. |
| 47 | + |
| 48 | +</Alert> |
| 49 | + |
| 50 | +<br /> |
| 51 | + |
| 52 | +## 3. Initialize the SDK with Environment Variables |
| 53 | + |
| 54 | +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. |
| 55 | + |
| 56 | +In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](/concepts/key-terms/tracing/). |
| 57 | + |
| 58 | +Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below. |
| 59 | + |
| 60 | +<OnboardingOptionButtons options={["error-monitoring", "performance"]} /> |
| 61 | + |
| 62 | +Set the following environment variables in your Lambda function configuration: |
| 63 | + |
| 64 | +```bash {"onboardingOptions": {"performance": "3"}} |
| 65 | +NODE_OPTIONS="-r @sentry/aws-serverless/awslambda-auto" |
| 66 | +SENTRY_DSN="___PUBLIC_DSN___" |
| 67 | +SENTRY_TRACES_SAMPLE_RATE="1.0" |
| 68 | +``` |
| 69 | + |
| 70 | +To set environment variables, navigate to your Lambda function, select **Configuration**, then **Environment variables**: |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +## Alternative: Initialize the SDK in Code |
| 75 | + |
| 76 | +Instead of [Step 3, setting environment variables](#3-initialize-the-sdk-with-environment-variables), you can also manually initialize the SDK in your Lambda function code. |
| 77 | +This way, you can customize the SDK setup further. |
| 78 | +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. |
| 79 | + |
| 80 | +Make sure you completed [step 1](#1-prerequisites) and [step 2](#2-add-the-sentry-lambda-layer) before proceeding. |
| 81 | + |
| 82 | +```javascript {filename:index.js} {"onboardingOptions": {"performance": "5-8"}} |
| 83 | +const Sentry = require("@sentry/aws-serverless"); |
| 84 | + |
| 85 | +Sentry.init({ |
| 86 | + dsn: "___PUBLIC_DSN___", |
| 87 | + // Add Tracing by setting tracesSampleRate and adding integration |
| 88 | + // Set tracesSampleRate to 1.0 to capture 100% of transactions |
| 89 | + // We recommend adjusting this value in production |
| 90 | + // Learn more at |
| 91 | + // https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate |
| 92 | + tracesSampleRate: 1.0, |
| 93 | +}); |
| 94 | + |
| 95 | +exports.handler = Sentry.wrapHandler(async (event, context) => { |
| 96 | + // Your handler code |
| 97 | +}); |
| 98 | +``` |
| 99 | + |
| 100 | +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. |
| 101 | + |
| 102 | +That's it - you're all set! |
0 commit comments