Skip to content

Commit d641ee2

Browse files
authored
docs(aws): Update guides for v10 Lambda Layer and NPM package (#14482)
_Draft: Merge when JS v10 SDKs are released in GA_ Introduces new unified docs for ESM and CJS versions of the Lambda layer and NPM package
1 parent f15e28a commit d641ee2

File tree

12 files changed

+498
-40
lines changed

12 files changed

+498
-40
lines changed

docs/platforms/javascript/guides/aws-lambda/index.mdx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ On this page you'll get an overview how to install, configure and use Sentry in
2020

2121
## Installation
2222

23-
Depending on your setup, there are different ways to install and use Sentry in your Lambda functions:
23+
Depending on your preferences, you can install Sentry in your Lambda functions using the [Sentry AWS Lambda Layer](./install/layer) (recommended) or the [Sentry AWS NPM package](./install/npm).
2424

25-
- If your Lambda functions are running in CommonJS (CJS) using `require` syntax, [install the Sentry AWS Lambda Layer](./install/cjs-layer)
26-
- If your Lambda functions are running in EcmaScript Modules (ESM) using `import` syntax, [install the Sentry AWS NPM package](./install/esm-npm)
25+
### Should I use the Lambda Layer or the NPM package?
2726

28-
If you're not sure which installation method to use or want an overview of all available options to use Sentry in your Lambda functions, read the [installation methods overview](./install).
27+
We generally recommend using the Lambda layer as it doesn't require you to deploy any Sentry dependency alongside your function.
28+
With the layer, you can achieve the same level of customization as with the NPM package.
29+
There are two reasons why you still might want to use the NPM package instead:
30+
31+
1. You want to minimize lambda function size and tree-shake parts of the SDK code that you don't need. A related reason might be because you're transpiling your code and want to transpile your dependencies as well.
32+
2. You already use NPM packages and deploy `node_modules` with your function and you don't want to add a (or another) Lambda layer to your functions.
2933

3034
## Configuration
3135

@@ -41,8 +45,10 @@ exports.handler = async (event, context) => {
4145
};
4246
```
4347

44-
```javascript {filename:index.mjs}{tabTitle:ESM}{2}
45-
export const handler = async (event, context) => {
48+
```javascript {filename:index.mjs}{tabTitle:ESM}{4}
49+
import * as Sentry from "@sentry/aws-serverless";
50+
51+
export const handler = Sentry.wrapHandler(async (event, context) => {
4652
throw new Error("This is a test error");
47-
};
53+
}));
4854
```
File renamed without changes.
File renamed without changes.
148 KB
Loading

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

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,12 @@ sidebar_order: 1
44
description: "Review all installation methods for using Sentry in AWS Lambda functions"
55
---
66

7-
8-
## Which Installation Method Should I Use?
9-
10-
Choosing the right installation method for using Sentry in your AWS Lambda functions depends on a couple of factors.
11-
12-
Lambda functions can be written and executed either in CommonJS, using `require` syntax, or EcmaScript Modules (ESM), using `import` syntax.
13-
For Sentry to work correctly, you need to follow the guide for the format in which your functions are **executed**.
14-
15-
### My Lambda function is written in TypeScript
16-
17-
If you're using TypeScript, your lambda function is likely transpiled to CommonJS before running it. In this case, follow the [CommonJS instructions](#my-lambda-function-uses-require).
18-
Note that TypeScript can also be configured to output ESM, in which case you should follow the [ESM instructions](#my-lambda-function-uses-import).
19-
20-
### My Lambda function uses `require`
21-
22-
If you are using `require()` in your function, follow the CommonJS instructions. Choose between [using our Lambda Layer (recommended)](./cjs-layer) or [installing the Sentry AWS NPM package](./cjs-layer).
23-
24-
### My Lambda function uses `import`
25-
26-
If you're using `import` syntax in your function and you're _not_ transpiling the code to CommonJS, follow the [ESM instructions](./esm-npm).
27-
28-
### Can I use the Lambda layer for ESM functions?
29-
30-
At this time, the Lambda Layer is only available for CommonJS functions. If you're using ESM, you'll need to set up Sentry manually.
31-
We're working on an ESM lambda layer but need to solve several OpenTelemetry-related limitations first.
7+
<PageGrid />
328

339
### Should I use the Lambda Layer or the NPM package?
3410

35-
First off, at this time, you can only use the layer in CommonJS functions. If you're using ESM, you'll need to set up Sentry manually with the NPM package.
36-
37-
We generally recommend to use the Lambda layer as it doesn't require you to deploy any Sentry dependency alongside your function.
38-
With using the layer, you can achieve the same level of customization as with the NPM package.
11+
We generally recommend using the Lambda layer as it doesn't require you to deploy any Sentry dependency alongside your function.
12+
With the layer, you can achieve the same level of customization as with the NPM package.
3913
There are two reasons why you still might want to use the NPM package instead:
4014

4115
1. You want to minimize lambda function size and tree-shake parts of the SDK code that you don't need. A related reason might be because you're transpiling your code and want to transpile your dependencies as well.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: Installation Methods
3+
sidebar_order: 1
4+
description: "Review all installation methods for using Sentry in AWS Lambda functions"
5+
---
6+
7+
## Which Installation Method Should I Use?
8+
9+
Choosing the right installation method for using Sentry in your AWS Lambda functions depends on a couple of factors.
10+
11+
Lambda functions can be written and executed either in CommonJS, using `require` syntax, or EcmaScript Modules (ESM), using `import` syntax.
12+
For Sentry to work correctly, you need to follow the guide for the format in which your functions are **executed**.
13+
14+
### My Lambda function is written in TypeScript
15+
16+
If you're using TypeScript, your lambda function is likely transpiled to CommonJS before running it. In this case, follow the [CommonJS instructions](#my-lambda-function-uses-require).
17+
Note that TypeScript can also be configured to output ESM, in which case you should follow the [ESM instructions](#my-lambda-function-uses-import).
18+
19+
### My Lambda function uses `require`
20+
21+
If you are using `require()` in your function, follow the CommonJS instructions. Choose between [using our Lambda Layer (recommended)](./install/layer__v9.x) or [installing the Sentry AWS NPM package](./install/cjs-npm__v9.x).
22+
23+
### My Lambda function uses `import`
24+
25+
If you're using `import` syntax in your function and you're _not_ transpiling the code to CommonJS, follow the [ESM instructions](./install/esm-npm__v9.x).
26+
27+
### Can I use the Lambda layer for ESM functions?
28+
29+
At this time, the Lambda Layer is only available for CommonJS functions. If you're using ESM, you'll need to set up Sentry manually.
30+
We're working on an ESM lambda layer but need to solve several OpenTelemetry-related limitations first.
31+
32+
### Should I use the Lambda Layer or the NPM package?
33+
34+
First off, at this time, you can only use the layer in CommonJS functions. If you're using ESM, you'll need to set up Sentry manually with the NPM package.
35+
36+
We generally recommend to use the Lambda layer as it doesn't require you to deploy any Sentry dependency alongside your function.
37+
With using the layer, you can achieve the same level of customization as with the NPM package.
38+
There are two reasons why you still might want to use the NPM package instead:
39+
40+
1. You want to minimize lambda function size and tree-shake parts of the SDK code that you don't need. A related reason might be because you're transpiling your code and want to transpile your dependencies as well.
41+
2. You already use NPM packages and deploy `node_modules` with your function and you don't want to add a (or another) Lambda layer to your functions.
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
![](./img/lambda_view.png)
22+
23+
**Specify an ARN** tab as illustrated:
24+
25+
![](./img/add_layer.png)
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+
![](./img/env_vars.png)
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!

docs/platforms/javascript/guides/aws-lambda/install/cjs-layer.mdx renamed to docs/platforms/javascript/guides/aws-lambda/install/layer__v9.x.mdx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ Add the Sentry Layer by navigating to your Lambda function. Select **Layers**, t
3131

3232
![](./img/add_layer.png)
3333

34-
Finally, set the region and copy the provided ARN value into the input.
34+
Modify and copy the ARN value for your region into the input, e.g. for region `us-west-1` and the current v9 Lambda layer version `56`:
3535

36-
<LambdaLayerDetail canonical="aws-layer:node" />
36+
```
37+
arn:aws:Lambda:us-west-1:943013980633:layer:SentryNodeServerlessSDKv9:56
38+
```
3739

3840
<br />
3941

@@ -74,12 +76,12 @@ const Sentry = require("@sentry/aws-serverless");
7476

7577
Sentry.init({
7678
dsn: "___PUBLIC_DSN___",
77-
79+
7880
// Adds request headers and IP for users, for more info visit:
7981
// https://docs.sentry.io/platforms/javascript/guides/aws-lambda/configuration/options/#sendDefaultPii
8082
sendDefaultPii: true,
8183
// ___PRODUCT_OPTION_START___ performance
82-
84+
8385
// Add Tracing by setting tracesSampleRate and adding integration
8486
// Set tracesSampleRate to 1.0 to capture 100% of transactions
8587
// We recommend adjusting this value in production

0 commit comments

Comments
 (0)