Skip to content

Commit f13e2f6

Browse files
Lms24andreiborza
andauthored
ref(aws-lambda): Restructure and improve AWS Lambda SDK documentation (#10655)
--------- Co-authored-by: Andrei <168741329+andreiborza@users.noreply.github.com>
1 parent e27b421 commit f13e2f6

File tree

15 files changed

+475
-203
lines changed

15 files changed

+475
-203
lines changed

docs/organization/integrations/cloud-monitoring/aws-lambda/index.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ description: "Learn more about Sentry's AWS Lambda integration and how you can a
66

77
<Note>
88

9-
Looking for instructions to set up the serverless SDK manually? Check out these docs for [Node](/platforms/javascript/guides/aws-lambda/) or [Python](/platforms/python/integrations/aws-lambda/) instead.
9+
This method of setting up Sentry in your Lambda functions only works if your Lambda functions are run in Node CommonJS (`require` syntax) or in Python.
10+
For all other setups, check out these docs for [Node](/platforms/javascript/guides/aws-lambda/) or [Python](/platforms/python/integrations/aws-lambda/) instead.
1011

1112
</Note>
1213

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: Lambda Handler Wrapper
3+
description: "Configure Sentry's Lambda function wrapper"
4+
sidebar_order: 2
5+
---
6+
7+
On this page you'll learn about the options to configure the `Sentry.wrapHandler` wrapper for your Lambda function.
8+
9+
If you're using the AWS Lambda layer with environment variables (i.e. no Sentry code in your function), skip this guide or switch to the [initializing the SDK in code](../../install/cjs-layer#alternative-initialize-the-sdk-in-code).
10+
11+
## Flush Timeout
12+
13+
Sentry keeps the lambda function thread alive for up to 2 seconds to ensure reported errors are sent. You can change this flush time limit by defining a `flushTimeout` value in the handler options:
14+
15+
```javascript {filename:index.js} {2}
16+
exports.handler = Sentry.wrapHandler(yourHandler, {
17+
flushTimeout: 1000,
18+
});
19+
```
20+
21+
## Timeout Warning
22+
23+
Sentry reports timeout warnings when the Lambda function is within 500ms of its execution time. You can turn off timeout warnings by setting `captureTimeoutWarning` to `false` in the handler options.
24+
25+
```javascript {filename:index.js} {2}
26+
exports.handler = Sentry.wrapHandler(yourHandler, {
27+
captureTimeoutWarning: false,
28+
});
29+
```
30+
31+
To change the timeout warning limit, assign a numeric value (in ms) to `timeoutWarningLimit`:
32+
33+
```javascript {filename:index.js} {2}
34+
exports.handler = Sentry.wrapHandler(yourHandler, {
35+
timeoutWarningLimit: 700,
36+
});
37+
```
38+
39+
## Capture Rejected Promises in `Promise.allSettled`
40+
41+
By default, Sentry captures errors raised by your handler.
42+
However, your handler might return a `Promise.allSettled` result.
43+
In this case, even if one of the messages has failed, Sentry won't capture any exception.
44+
45+
The `captureAllSettledReasons` (default: `false`) option captures all promise rejected results
46+
47+
```javascript {tabTitle:captureAllSettledReasons}
48+
exports.handler = Sentry.wrapHandler(
49+
() => {
50+
return Promise.allSettled([
51+
Promise.rejected(new Error("first")),
52+
Promise.rejected(new Error("second")),
53+
]);
54+
},
55+
{ captureAllSettledReasons: true }
56+
);
57+
// `first` and `second` errors are captured
58+
```

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

Lines changed: 0 additions & 43 deletions
This file was deleted.

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

Lines changed: 18 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -13,115 +13,33 @@ Looking for instructions on how to add Sentry without modifying your code? [Chec
1313

1414
</Note>
1515

16-
Before you begin, note:
16+
On this page you'll get an overview how to install, configure and use Sentry in your AWS Lambda functions. Once set up, our SDK will automatically report error and performance data from your Lambda Functions. Issues in Sentry will automatically include cloudwatch data, function details and execution time measurements.
1717

18-
- The estimated time to configure the integration is 5 - 15 minutes.
19-
- You need to have experience with AWS console and a basic understanding of Lambda.
20-
- You'll need an AWS account and you have to create an [IAM user](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html).
18+
## Installation
2119

22-
<Note>This guide is for version 8.0.0 and up of `@sentry/aws-serverless`.</Note>
20+
Depending on your setup, there are different ways to install and use Sentry in your Lambda functions. We recommend one of the following options:
2321

24-
## Install
22+
- [Install the Sentry AWS Lambda Layer](./install/cjs-layer) if your Lambda functions are written in CommonJS (CJS) using `require` syntax.
23+
- [Install the Sentry AWS NPM package](./install/esm-npm) if your Lambda functions are running in EcmaScript Modules (ESM) using `import` syntax.
2524

26-
<OnboardingOptionButtons
27-
options={["error-monitoring", "performance", "profiling"]}
28-
/>
25+
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](/guides/aws-lambda/install).
2926

30-
Create a deployment package on your local machine and install the required dependencies in the deployment package. For more information, see [Building an AWS Lambda deployment package for Node.js](https://aws.amazon.com/premiumsupport/knowledge-center/lambda-deployment-package-nodejs/).
27+
## Configuration
3128

32-
<PlatformContent includePath="getting-started-install" />
29+
After installing the SDK, you might want to configure some parameters. Besides the [common SDK configuration](./configuration/), you can also [configure Sentry's Lambda Function handler wrapper](./configuration/lambda-wrapper) to optimize function runtime overhead and timeout warnings.
3330

34-
We also support [installing Sentry in Lambda Layer](/platforms/javascript/guides/aws-lambda/layer/).
31+
## Verify
3532

36-
## Configure
33+
Once set up, verify that Sentry is reporting errors correctly by throwing a sample error in one of your functions:
3734

38-
You can use the AWS Lambda integration for the Node like this:
39-
40-
<SignInNote />
41-
42-
```javascript {tabTitle:async}
43-
const Sentry = require("@sentry/aws-serverless");
44-
45-
Sentry.init({
46-
dsn: "___PUBLIC_DSN___",
47-
48-
// Add Tracing by setting tracesSampleRate and adding integration
49-
// Set tracesSampleRate to 1.0 to capture 100% of transactions
50-
// We recommend adjusting this value in production
51-
tracesSampleRate: 1.0,
52-
});
53-
54-
exports.handler = Sentry.wrapHandler(async (event, context) => {
55-
// Your handler code
56-
});
35+
```javascript {filename:index.js}{tabTitle:CommonJS}{2}
36+
exports.handler = async (event, context) => {
37+
throw new Error("This is a test error");
38+
};
5739
```
5840

59-
```javascript {tabTitle:sync}
60-
const Sentry = require("@sentry/aws-serverless");
61-
62-
Sentry.init({
63-
dsn: "___PUBLIC_DSN___",
64-
65-
// We recommend adjusting this value in production, or using tracesSampler
66-
// for finer control
67-
tracesSampleRate: 1.0,
68-
});
69-
70-
exports.handler = Sentry.wrapHandler((event, context, callback) => {
71-
// Your handler code
72-
});
41+
```javascript {filename:index.mjs}{tabTitle:ESM}{2}
42+
export const handler = async (event, context) => {
43+
throw new Error("This is a test error");
44+
};
7345
```
74-
75-
{/* <!-- TODO-ADD-VERIFICATION-EXAMPLE --> */}
76-
77-
## Enable Timeout Warning
78-
79-
Sentry reports timeout warning when the function is within 500ms of its execution time. You can turn off timeout warnings by setting `captureTimeoutWarning` to `false` in the handler options. To change timeout warning limit, assign a numeric value (in ms) to `timeoutWarningLimit`
80-
81-
```javascript {tabTitle:captureTimeoutWarning}
82-
exports.handler = Sentry.wrapHandler(yourHandler, {
83-
captureTimeoutWarning: false,
84-
});
85-
```
86-
87-
```javascript {tabTitle:timeoutWarning}
88-
exports.handler = Sentry.wrapHandler(yourHandler, {
89-
timeoutWarningLimit: 50,
90-
});
91-
```
92-
93-
## Capture Rejected Promises in `Promise.allSettled`
94-
95-
By default, Sentry captures errors raised by your handler.
96-
However, your handler might return a `Promise.allSettled` result.
97-
In this case, even if one of the messages has failed, Sentry won't capture any exception.
98-
99-
The `captureAllSettledReasons` (default: `false`) option captures all promise rejected results
100-
101-
```javascript {tabTitle:captureAllSettledReasons}
102-
exports.handler = Sentry.wrapHandler(
103-
() => {
104-
return Promise.allSettled([
105-
Promise.rejected(new Error("first")),
106-
Promise.rejected(new Error("second")),
107-
]);
108-
},
109-
{ captureAllSettledReasons: true }
110-
);
111-
// `first` and `second` errors are captured
112-
```
113-
114-
## Behavior
115-
116-
With the AWS Lambda integration enabled, the Node SDK will:
117-
118-
- Automatically report all events from your Lambda Functions.
119-
- Allows you to <PlatformLink to="/configuration/sampling/#configuring-the-transaction-sample-rate">modify the transaction sample rate</PlatformLink> using <PlatformIdentifier name="traces-sample-rate" />.
120-
- Issue reports automatically include:
121-
- A link to the cloudwatch logs
122-
- Function details
123-
- sys.argv for the function
124-
- AWS Request ID
125-
- Function execution time
126-
- Function version
127-
- Sentry holds the thread for up to 2 seconds to report errors. You can change flush time limit by defining a `flushTimeout` value in the handler options
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
---
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/configuration-layers.html) instead of adding `@sentry/aws-serverless` with `npm` or `yarn` [manually](../cjs-manual).
8+
If you follow this guide, you don't have to worry about deploying Sentry dependencies alongside your function code.
9+
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.
10+
11+
<Note>
12+
13+
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).
14+
15+
</Note>
16+
17+
## 1. Prerequisites
18+
19+
Before you begin, make sure you have the following:
20+
21+
- You have a Lambda function that is running in CommonJS (CJS) mode, using `require` syntax.
22+
- You know the AWS region that your function is deployed to.
23+
24+
## 2. Add the Sentry Lambda Layer
25+
26+
Add the Sentry Layer by navigating to your Lambda function. Select **Layers**, then **Add a Layer**.
27+
28+
![](./img/lambda_view.png)
29+
30+
**Specify an ARN** tab as illustrated:
31+
32+
![](./img/add_layer.png)
33+
34+
Finally, set the region and copy the provided ARN value into the input.
35+
36+
<LambdaLayerDetail canonical="aws-layer:node" />
37+
38+
<br />
39+
40+
## 3. Initialize the SDK with Environment Variables
41+
42+
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.
43+
44+
<OnboardingOptionButtons options={["error-monitoring", "performance"]} />
45+
46+
Set the following environment variables in your Lambda function configuration:
47+
48+
```bash {"onboardingOptions": {"performance": "3"}}
49+
NODE_OPTIONS="-r @sentry/aws-serverless/awslambda-auto"
50+
SENTRY_DSN="___PUBLIC_DSN___"
51+
SENTRY_TRACES_SAMPLE_RATE="1.0"
52+
```
53+
54+
To set environment variables, navigate to your Lambda function, select **Configuration**, then **Environment variables**:
55+
56+
![](./img/cjs_env_vars.png)
57+
58+
## Alternative: Initialize the SDK in Code
59+
60+
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.
61+
This way, you can customize the SDK setup further.
62+
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.
63+
64+
Make sure you completed [step 1](#1-prerequisites) and [step 2](#2-add-the-sentry-lambda-layer) before proceeding.
65+
66+
```javascript {filename:index.js} {"onboardingOptions": {"performance": "5-8"}}
67+
const Sentry = require("@sentry/aws-serverless");
68+
69+
Sentry.init({
70+
dsn: "__PUBLIC_DSN__",
71+
// Add Tracing by setting tracesSampleRate and adding integration
72+
// Set tracesSampleRate to 1.0 to capture 100% of transactions
73+
// We recommend adjusting this value in production
74+
tracesSampleRate: 1.0,
75+
});
76+
77+
exports.handler = Sentry.wrapHandler(async (event, context) => {
78+
// Your handler code
79+
});
80+
```
81+
82+
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.
83+
84+
That's it - you're all set!
85+
86+
## Lambda layer for v7 SDK
87+
88+
The instructions above are written for SDK version 8 (the most recent version).
89+
You can also install a v7 version of the Sentry Lambda layer in case you can't upgrade to v8.
90+
The procedure is identical to the instructions above except for two differences:
91+
92+
### v7 layer ARN
93+
94+
The v7 Lambda layer has a different ARN:
95+
96+
```
97+
arn:aws:Lambda:us-west-1:943013980633:layer:SentryNodeServerlessSDKv7:3
98+
```
99+
100+
Modify and copy the ARN value for your region into the input, e.g. for region `:us-west-1` and the current v7 Lambda layer version `:3`:
101+
102+
### v7 package name
103+
104+
The `@sentry/aws-serverless` package was called `@sentry/serverless` prior to version 8. Therefore, for the v7 layer, adjust your `NODE_OPTIONS` environment variable:
105+
106+
```bash
107+
NODE_OPTIONS="-r @sentry/serverless/dist/awslambda-auto"
108+
```
109+
110+
The other environment variables remain the same as above.

0 commit comments

Comments
 (0)