|
| 1 | +--- |
| 2 | +title: Lambda Layer |
| 3 | +description: "Learn how to add the Sentry Node Lambda Layer to use Sentry in your Lambda functions" |
| 4 | +sidebar_order: 1 |
| 5 | +--- |
| 6 | + |
| 7 | +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 installing `@sentry/aws-serverless` with a package manager manually. |
| 8 | +If you follow this guide, you don't have to worry about deploying Sentry dependencies alongside your function code. |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +Before you begin, make sure you have the following: |
| 13 | + |
| 14 | +- You have a Lambda function deployed in AWS. |
| 15 | +- You know the AWS region that your function is deployed to. |
| 16 | + |
| 17 | +## 1. Add the Sentry Lambda Layer |
| 18 | + |
| 19 | +Add the Sentry Layer by navigating to your Lambda function. Select **Layers**, then **Add a Layer**. |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +**Specify an ARN** tab as illustrated: |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +Finally, set the region and copy the provided ARN value into the input. |
| 28 | + |
| 29 | +<LambdaLayerDetail canonical="aws-layer:node" /> |
| 30 | + |
| 31 | +<br /> |
| 32 | + |
| 33 | +## 2. Setup Options |
| 34 | + |
| 35 | +Choose your setup method based on your Lambda function type: |
| 36 | + |
| 37 | +<Alert level="info" title="ESM vs. CommonJS"> |
| 38 | + |
| 39 | +The setup instructions you should follow depend on how your function **runs** at runtime, not how it's written in your source code. |
| 40 | + |
| 41 | +- **Use CommonJS instructions** if your function runs with CommonJS modules (uses `require()` and `module.exports` at runtime) |
| 42 | +- **Use ESM instructions** if your function runs with ES modules (uses `import`/`export` at runtime) |
| 43 | + |
| 44 | +**Important:** Even if you write your code with `import`/`export` syntax, your function might still run as CommonJS if you're using TypeScript or a build tool that compiles to CommonJS. Check your build output or Lambda runtime configuration to determine which applies to your function. |
| 45 | + |
| 46 | +</Alert> |
| 47 | + |
| 48 | +### Option A: Automatic Setup |
| 49 | + |
| 50 | +**CommonJS functions** support fully automatic setup using environment variables - both SDK initialization and handler wrapping are handled automatically. |
| 51 | + |
| 52 | +**ESM functions** support automatic SDK initialization via environment variables, but require manual handler wrapping. |
| 53 | + |
| 54 | +In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](/concepts/key-terms/tracing/). |
| 55 | + |
| 56 | +Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below. |
| 57 | + |
| 58 | +<OnboardingOptionButtons options={["error-monitoring", "performance"]} /> |
| 59 | + |
| 60 | +Set the following environment variables in your Lambda function configuration: |
| 61 | + |
| 62 | +```bash {tabTitle:CommonJS} |
| 63 | +NODE_OPTIONS="--require @sentry/aws-serverless/awslambda-auto" |
| 64 | +SENTRY_DSN="___PUBLIC_DSN___" |
| 65 | +# ___PRODUCT_OPTION_START___ performance |
| 66 | +SENTRY_TRACES_SAMPLE_RATE="1.0" |
| 67 | +# ___PRODUCT_OPTION_END___ performance |
| 68 | +``` |
| 69 | + |
| 70 | +```bash {tabTitle:ESM} |
| 71 | +NODE_OPTIONS="--import @sentry/aws-serverless/awslambda-auto" |
| 72 | +SENTRY_DSN="___PUBLIC_DSN___" |
| 73 | +# ___PRODUCT_OPTION_START___ performance |
| 74 | +SENTRY_TRACES_SAMPLE_RATE="1.0" |
| 75 | +# ___PRODUCT_OPTION_END___ performance |
| 76 | +``` |
| 77 | + |
| 78 | +To set environment variables, navigate to your Lambda function, select **Configuration**, then **Environment variables**: |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +<Alert level="info" title="For ESM Lambda Functions"> |
| 83 | + |
| 84 | +You'll also need to manually wrap your handler as shown below: |
| 85 | + |
| 86 | +```javascript {filename:index.mjs} |
| 87 | +import * as Sentry from "@sentry/aws-serverless"; |
| 88 | + |
| 89 | +export const handler = Sentry.wrapHandler(async (event, context) => { |
| 90 | + // Your handler code |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +</Alert> |
| 95 | + |
| 96 | +### Option B: Manual Setup |
| 97 | + |
| 98 | +Instead of using environment variables, you can manually initialize the SDK and wrap your handler in code. This approach works for both CommonJS and ESM functions and allows for further customization of the SDK setup. |
| 99 | + |
| 100 | +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. |
| 101 | + |
| 102 | +#### For CommonJS Lambda Functions |
| 103 | + |
| 104 | +```javascript {filename:index.js} |
| 105 | +const Sentry = require("@sentry/aws-serverless"); |
| 106 | + |
| 107 | +Sentry.init({ |
| 108 | + dsn: "___PUBLIC_DSN___", |
| 109 | + |
| 110 | + // Adds request headers and IP for users, for more info visit: |
| 111 | + // https://docs.sentry.io/platforms/javascript/guides/aws-lambda/configuration/options/#sendDefaultPii |
| 112 | + sendDefaultPii: true, |
| 113 | + // ___PRODUCT_OPTION_START___ performance |
| 114 | + |
| 115 | + // Add Tracing by setting tracesSampleRate and adding integration |
| 116 | + // Set tracesSampleRate to 1.0 to capture 100% of transactions |
| 117 | + // We recommend adjusting this value in production |
| 118 | + // Learn more at |
| 119 | + // https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate |
| 120 | + tracesSampleRate: 1.0, |
| 121 | + // ___PRODUCT_OPTION_END___ performance |
| 122 | +}); |
| 123 | + |
| 124 | +// Your package imports |
| 125 | + |
| 126 | +exports.handler = Sentry.wrapHandler(async (event, context) => { |
| 127 | + // Your handler code |
| 128 | +}); |
| 129 | +``` |
| 130 | + |
| 131 | +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. Make sure that the `Sentry.init` call and the import statement are at the very top of your file before any other imports. |
| 132 | + |
| 133 | +#### For ESM Lambda Functions |
| 134 | + |
| 135 | +First, wrap your handler: |
| 136 | + |
| 137 | +```javascript {filename:index.mjs}{1,3} |
| 138 | +import * as Sentry from "@sentry/aws-serverless"; |
| 139 | + |
| 140 | +export const handler = Sentry.wrapHandler(async (event, context) => { |
| 141 | + // Your handler code |
| 142 | +}); |
| 143 | +``` |
| 144 | + |
| 145 | +Due to ESM limitations, you need to initialize the SDK in a separate file and load it before your function starts. |
| 146 | + |
| 147 | +Create a new file, for example `instrument.mjs` to initialize the SDK: |
| 148 | + |
| 149 | +```javascript {filename:instrument.mjs} |
| 150 | +import * as Sentry from "@sentry/aws-serverless"; |
| 151 | + |
| 152 | +Sentry.init({ |
| 153 | + dsn: "___PUBLIC_DSN___", |
| 154 | + |
| 155 | + // Adds request headers and IP for users, for more info visit: |
| 156 | + // https://docs.sentry.io/platforms/javascript/guides/aws-lambda/configuration/options/#sendDefaultPii |
| 157 | + sendDefaultPii: true, |
| 158 | + // ___PRODUCT_OPTION_START___ performance |
| 159 | + |
| 160 | + // Add Tracing by setting tracesSampleRate and adding integration |
| 161 | + // Set tracesSampleRate to 1.0 to capture 100% of transactions |
| 162 | + // We recommend adjusting this value in production |
| 163 | + // Learn more at |
| 164 | + // https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate |
| 165 | + tracesSampleRate: 1.0, |
| 166 | + // ___PRODUCT_OPTION_END___ performance |
| 167 | +}); |
| 168 | +``` |
| 169 | + |
| 170 | +##### Load the SDK |
| 171 | + |
| 172 | +To load the SDK before your function starts, you need to preload the `instrument.mjs` by setting the `NODE_OPTIONS` environment variable: |
| 173 | + |
| 174 | +```bash |
| 175 | +NODE_OPTIONS="--import ./instrument.mjs" |
| 176 | +``` |
| 177 | + |
| 178 | +That's it — make sure to re-deploy your function and you're all set! |
0 commit comments