Skip to content

Commit 8039fc2

Browse files
authored
feat(aws): Add alternative way of initializing Sentry for NPM + CJS (#13897)
This aligns the documentation with the ESM instructions, showing how to `--require` an instrument file.
1 parent d418cec commit 8039fc2

File tree

4 files changed

+76
-14
lines changed

4 files changed

+76
-14
lines changed

docs/platforms/javascript/guides/aws-lambda/install/cjs-npm.mdx

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ sidebar_order: 2
55
---
66

77
In this guide you will learn how to set up the `@sentry/aws-serverless` SDK for AWS Lambda functions running in CommonJS (CJS) mode.
8-
This installation method allows you to fully customize your Sentry SDK setup while also being able to tree-shake, transpile and bundle the SDK Code. However, you need to modify your code and deploy the Sentry dependencies alongside your function code. If you're looking for the most simple way to set up Sentry, you might want to use the [Lambda Layer](../cjs-layer) instead.
8+
We recommend starting the SDK automatically via environment variables so that you only have to make minimal code changes to your lambda function.
9+
If you need more control over the SDK setup, you can also [initialize the SDK in in code](#alternative-initialize-the-sdk-in-code).
10+
11+
However, you need to modify your code and deploy the Sentry dependencies alongside your function code. If you're looking for the most simple way to set up Sentry, you might want to use the [Lambda Layer](../cjs-layer) instead.
912

1013
## 1. Prerequisites
1114

@@ -58,27 +61,66 @@ pnpm add @sentry/aws-serverless @sentry/profiling-node
5861

5962
</OnboardingOption>
6063

61-
## 3. Configure
64+
## 3. Wrap your handler
6265

63-
Initialize and configure the SDK in your Lambda function Code. Call `Sentry.init` _outside_ of the handler function to avoid re-initializing the SDK on every invocation. Also, add the `Sentry.wrapHandler` wrapper around your function to automatically catch errors and performance data:
66+
Add the `Sentry.wrapHandler` wrapper around your function handler to automatically catch errors and performance data:
6467

65-
```javascript {filename:index.js}
68+
```javascript {filename:index.js}{1, 3}
69+
const Sentry = require("@sentry/aws-serverless");
70+
71+
exports.handler = Sentry.wrapHandler(async (event, context) => {
72+
// Your handler code
73+
});
74+
```
75+
76+
## 4. Set Environment Variables
77+
78+
To initialize and configure the SDK, you need to set the following environment variables in your AWS Lambda function configuration:
79+
80+
```bash
81+
NODE_OPTIONS="-r @sentry/aws-serverless/awslambda-auto"
82+
SENTRY_DSN="___PUBLIC_DSN___"
83+
# ___PRODUCT_OPTION_START___ performance
84+
SENTRY_TRACES_SAMPLE_RATE="1.0"
85+
# ___PRODUCT_OPTION_END___ performance
86+
```
87+
88+
To set environment variables, navigate to your Lambda function, select **Configuration**, then **Environment variables**:
89+
90+
![](./img/cjs_env_vars.png)
91+
92+
That's it - make sure to re-deploy your function and you're all set!
93+
94+
## Alternative: Initialize the SDK in Code
95+
96+
To further customize the SDK setup, you can also manually initialize the SDK in your lambda function. You need to initialize the SDK in a separate file and load it before your function starts.
97+
The benefit of this installation method is that you can fully customize your Sentry SDK setup in a `Sentry.init` call.
98+
99+
### 1. Install
100+
101+
Follow steps 1 and 2 above to [install the SDK NPM package](#2-install) in your project.
102+
103+
### 2. Initialize the SDK
104+
105+
Create a new file, for example `instrument.js` to initialize the SDK:
106+
107+
```javascript {filename:instrument.js}
66108
const Sentry = require("@sentry/aws-serverless");
67109
// ___PRODUCT_OPTION_START___ profiling
68110
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
69111

70112
// ___PRODUCT_OPTION_END___ profiling
71113
Sentry.init({
72114
dsn: "___PUBLIC_DSN___",
73-
115+
74116
// Adds request headers and IP for users, for more info visit:
75117
// https://docs.sentry.io/platforms/javascript/guides/aws-lambda/configuration/options/#sendDefaultPii
76118
sendDefaultPii: true,
77-
119+
78120
// ___PRODUCT_OPTION_START___ profiling
79121
integrations: [nodeProfilingIntegration()],
80122
// ___PRODUCT_OPTION_END___ profiling
81-
123+
82124
// ___PRODUCT_OPTION_START___ performance
83125
// Add Tracing by setting tracesSampleRate and adding integration
84126
// Set tracesSampleRate to 1.0 to capture 100% of transactions
@@ -88,20 +130,40 @@ Sentry.init({
88130
tracesSampleRate: 1.0,
89131
// ___PRODUCT_OPTION_END___ performance
90132
// ___PRODUCT_OPTION_START___ profiling
91-
133+
92134
// Set sampling rate for profiling - this is relative to tracesSampleRate
93135
profilesSampleRate: 1.0,
94136
// ___PRODUCT_OPTION_END___ profiling
95137
});
138+
```
139+
140+
### 3. Wrap your handler
141+
142+
Add the `Sentry.wrapHandler` wrapper around your function handler to automatically catch errors and performance data:
143+
144+
```javascript {filename:index.js}{1,3}
145+
const Sentry = require("@sentry/aws-serverless");
96146

97147
exports.handler = Sentry.wrapHandler(async (event, context) => {
98148
// Your handler code
99149
});
100150
```
101151

152+
### 4. Load the SDK
153+
154+
To load the SDK before your function starts, you need to preload the `instrument.js` by setting the `NODE_OPTIONS` environment variable:
155+
156+
```bash
157+
NODE_OPTIONS="--require ./instrument.js"
158+
```
159+
160+
To set environment variables, navigate to your Lambda function, select **Configuration**, then **Environment variables**:
161+
162+
![](./img/cjs_manual_env_vars.png)
163+
102164
That's it - make sure to re-deploy your function and you're all set!
103165

104166
## Using the v7 SDK
105167

106-
The instructions above are written for SDK version 8 (the most recent version).
168+
The instructions above are written for the latest SDK version.
107169
In SDK versions prior to version 8, the `@sentry/aws-serverless` package was called `@sentry/serverless`. If you are using an older version, you can follow this guide but replace the package with `@sentry/serverless`.

docs/platforms/javascript/guides/aws-lambda/install/esm-npm.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,24 @@ import { nodeProfilingIntegration } from "@sentry/profiling-node";
117117
// ___PRODUCT_OPTION_END___ profiling
118118
Sentry.init({
119119
dsn: "___PUBLIC_DSN___",
120-
120+
121121
// Adds request headers and IP for users, for more info visit:
122122
// https://docs.sentry.io/platforms/javascript/guides/aws-lambda/configuration/options/#sendDefaultPii
123123
sendDefaultPii: true,
124124
// ___PRODUCT_OPTION_START___ profiling
125-
125+
126126
integrations: [nodeProfilingIntegration()],
127127
// ___PRODUCT_OPTION_END___ profiling
128128
// ___PRODUCT_OPTION_START___ performance
129-
129+
130130
// Add Tracing by setting tracesSampleRate and adding integration
131131
// Set tracesSampleRate to 1.0 to capture 100% of transactions
132132
// We recommend adjusting this value in production
133133
// Learn more at
134134
// https://docs.sentry.io/platforms/javascript/configuration/options/#traces-sample-rate
135135
tracesSampleRate: 1.0,
136136
// ___PRODUCT_OPTION_END___ performance
137-
137+
138138
// ___PRODUCT_OPTION_START___ profiling
139139
// Set sampling rate for profiling - this is relative to tracesSampleRate
140140
profilesSampleRate: 1.0,
@@ -170,4 +170,4 @@ That's it - make sure to re-deploy your function and you're all set!
170170

171171
## Using the v7 SDK
172172

173-
The v7 `@sentry/serverless` SDK does not work correctly with ESM-based Lambda functions. Please upgrade to the v8 SDK and follow the instructions above.
173+
The v7 `@sentry/serverless` SDK does not work correctly with ESM-based Lambda functions. Please upgrade to the latest SDK and follow the instructions above.
115 KB
Loading
7.84 KB
Loading

0 commit comments

Comments
 (0)