Skip to content

Commit b20a915

Browse files
authored
docs(aws): Remove manual setup steps for ESM (#14738)
Updates the docs to reflect that manual wrapping is not needed anymore for both CJS and ESM.
1 parent a14b1a8 commit b20a915

File tree

2 files changed

+18
-146
lines changed

2 files changed

+18
-146
lines changed

docs/platforms/javascript/guides/aws-lambda/install/layer.mdx

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,7 @@ Finally, set the region and copy the provided ARN value into the input.
3232

3333
## 2. Setup Options
3434

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.
35+
### Option A: Automatic Setup (recommended)
5336

5437
In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](/concepts/key-terms/tracing/).
5538

@@ -59,15 +42,7 @@ Select which Sentry features you'd like to install in addition to Error Monitori
5942

6043
Set the following environment variables in your Lambda function configuration:
6144

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}
45+
```bash
7146
NODE_OPTIONS="--import @sentry/aws-serverless/awslambda-auto"
7247
SENTRY_DSN="___PUBLIC_DSN___"
7348
# ___PRODUCT_OPTION_START___ performance
@@ -79,29 +54,15 @@ To set environment variables, navigate to your Lambda function, select **Configu
7954

8055
![](./img/env_vars.png)
8156

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-
9657
### Option B: Manual Setup
9758

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.
59+
To further customize the SDK setup, you can also manually initialize the SDK in your lambda function. The benefit of this installation method is that you can fully customize your Sentry SDK setup in a `Sentry.init` call.
9960

10061
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.
10162

102-
#### For CommonJS Lambda Functions
63+
Create a new file, for example `instrument.js` to initialize the SDK:
10364

104-
```javascript {filename:index.js}
65+
```javascript {filename:instrument.js} {tabTitle:CommonJS}
10566
const Sentry = require("@sentry/aws-serverless");
10667

10768
Sentry.init({
@@ -120,33 +81,9 @@ Sentry.init({
12081
tracesSampleRate: 1.0,
12182
// ___PRODUCT_OPTION_END___ performance
12283
});
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-
});
14384
```
14485

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}
86+
```javascript {filename:instrument.mjs} {tabTitle:ESM}
15087
import * as Sentry from "@sentry/aws-serverless";
15188

15289
Sentry.init({
@@ -169,10 +106,10 @@ Sentry.init({
169106

170107
##### Load the SDK
171108

172-
To load the SDK before your function starts, you need to preload the `instrument.mjs` by setting the `NODE_OPTIONS` environment variable:
109+
To load the SDK before your function starts, you need to preload the `instrument.js` by setting the `NODE_OPTIONS` environment variable:
173110

174111
```bash
175-
NODE_OPTIONS="--import ./instrument.mjs"
112+
NODE_OPTIONS="--import ./instrument.js"
176113
```
177114

178115
That's it — make sure to re-deploy your function and you're all set!

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

Lines changed: 10 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,6 @@ Before you begin, make sure you have the following:
1717
- You have a Lambda function deployed in AWS.
1818
- You're able to deploy dependencies (i.e. `node_modules`) alongside your function code to AWS Lambda.
1919

20-
<Alert level="info" title="ESM vs. CommonJS">
21-
22-
The setup instructions you should follow depend on how your Lambda function **runs** at runtime, not how it's written in your source code.
23-
24-
- **Use CommonJS instructions** if your function runs with CommonJS modules (uses `require()` and `module.exports` at runtime)
25-
- **Use ESM instructions** if your function runs with ES modules (uses `import`/`export` at runtime)
26-
27-
**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.
28-
29-
</Alert>
30-
3120
## 2. Install
3221

3322
In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](/concepts/key-terms/tracing/). You can also collect and analyze performance profiles from real users with [profiling](/product/explore/profiling/).
@@ -74,25 +63,11 @@ pnpm add @sentry/aws-serverless @sentry/profiling-node
7463

7564
## 3. Setup Options
7665

77-
Choose your setup method based on your Lambda function type:
78-
79-
### Option A: Automatic Setup
80-
81-
**CommonJS functions** support fully automatic setup using environment variables - both SDK initialization and handler wrapping are handled automatically.
82-
83-
**ESM functions** support automatic SDK initialization via environment variables, but require manual handler wrapping.
66+
### Option A: Automatic Setup (recommended)
8467

8568
Set the following environment variables in your Lambda function configuration:
8669

87-
```bash {tabTitle:CommonJS}
88-
NODE_OPTIONS="--require @sentry/aws-serverless/awslambda-auto"
89-
SENTRY_DSN="___PUBLIC_DSN___"
90-
# ___PRODUCT_OPTION_START___ performance
91-
SENTRY_TRACES_SAMPLE_RATE="1.0"
92-
# ___PRODUCT_OPTION_END___ performance
93-
```
94-
95-
```bash {tabTitle:ESM}
70+
```bash
9671
NODE_OPTIONS="--import @sentry/aws-serverless/awslambda-auto"
9772
SENTRY_DSN="___PUBLIC_DSN___"
9873
# ___PRODUCT_OPTION_START___ performance
@@ -104,36 +79,20 @@ To set environment variables, navigate to your Lambda function, select **Configu
10479

10580
![](./img/env_vars.png)
10681

107-
<Alert level="info" title="For ESM Lambda Functions">
108-
109-
You'll also need to manually wrap your handler as shown below:
110-
111-
```javascript {tabTitle:ESM} {filename:index.mjs}{1,3}
112-
import * as Sentry from "@sentry/aws-serverless";
113-
114-
export const handler = Sentry.wrapHandler(async (event, context) => {
115-
// Your handler code
116-
});
117-
```
118-
119-
</Alert>
120-
12182
That's it - make sure to re-deploy your function and you're all set!
12283

12384
### Option B: Manual Setup
12485

12586
To further customize the SDK setup, you can also manually initialize the SDK in your lambda function. The benefit of this installation method is that you can fully customize your Sentry SDK setup in a `Sentry.init` call.
12687

127-
#### For CommonJS Lambda Functions
128-
129-
You can initialize the SDK directly in your main handler file:
88+
Create a new file, for example `instrument.js` to initialize the SDK:
13089

131-
```javascript {tabTitle:CommonJS} {filename:index.js}
90+
```javascript {filename:instrument.js} {tabTitle:CommonJS}
13291
const Sentry = require("@sentry/aws-serverless");
13392
// ___PRODUCT_OPTION_START___ profiling
13493
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
135-
13694
// ___PRODUCT_OPTION_END___ profiling
95+
13796
Sentry.init({
13897
dsn: "___PUBLIC_DSN___",
13998

@@ -158,33 +117,9 @@ Sentry.init({
158117
profilesSampleRate: 1.0,
159118
// ___PRODUCT_OPTION_END___ profiling
160119
});
161-
162-
// Your package imports
163-
164-
exports.handler = Sentry.wrapHandler(async (event, context) => {
165-
// Your handler code
166-
});
167-
```
168-
169-
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.
170-
171-
#### For ESM Lambda Functions
172-
173-
First, wrap your handler:
174-
175-
```javascript {tabTitle:ESM} {filename:index.mjs}{1,3}
176-
import * as Sentry from "@sentry/aws-serverless";
177-
178-
export const handler = Sentry.wrapHandler(async (event, context) => {
179-
// Your handler code
180-
});
181120
```
182121

183-
Due to ESM limitations, you need to initialize the SDK in a separate file and load it before your function starts.
184-
185-
Create a new file, for example `instrument.mjs` to initialize the SDK:
186-
187-
```javascript {tabTitle:ESM} {filename:instrument.mjs}
122+
```javascript {filename:instrument.mjs} {tabTitle:ESM}
188123
import * as Sentry from "@sentry/aws-serverless";
189124
// ___PRODUCT_OPTION_START___ profiling
190125
import { nodeProfilingIntegration } from "@sentry/profiling-node";
@@ -198,9 +133,9 @@ Sentry.init({
198133
sendDefaultPii: true,
199134
// ___PRODUCT_OPTION_START___ profiling
200135
integrations: [nodeProfilingIntegration()],
136+
201137
// ___PRODUCT_OPTION_END___ profiling
202138
// ___PRODUCT_OPTION_START___ performance
203-
204139
// Add Tracing by setting tracesSampleRate and adding integration
205140
// Set tracesSampleRate to 1.0 to capture 100% of transactions
206141
// We recommend adjusting this value in production
@@ -218,10 +153,10 @@ Sentry.init({
218153

219154
##### Load the SDK
220155

221-
To load the SDK before your function starts, you need to set the `NODE_OPTIONS` environment variable:
156+
To load the SDK before your function starts, you need to preload the `instrument.js` by setting the `NODE_OPTIONS` environment variable:
222157

223-
```bash {tabTitle:ESM}
224-
NODE_OPTIONS="--import ./instrument.mjs"
158+
```bash
159+
NODE_OPTIONS="--import ./instrument.js"
225160
```
226161

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

0 commit comments

Comments
 (0)