Skip to content

Commit

Permalink
ref(aws-lambda): Restructure and improve AWS Lambda SDK documentation (
Browse files Browse the repository at this point in the history
…#10655)


---------

Co-authored-by: Andrei <168741329+andreiborza@users.noreply.github.com>
  • Loading branch information
Lms24 and andreiborza committed Jul 23, 2024
1 parent e27b421 commit f13e2f6
Show file tree
Hide file tree
Showing 15 changed files with 475 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ description: "Learn more about Sentry's AWS Lambda integration and how you can a

<Note>

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.
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.
For all other setups, check out these docs for [Node](/platforms/javascript/guides/aws-lambda/) or [Python](/platforms/python/integrations/aws-lambda/) instead.

</Note>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
title: Lambda Handler Wrapper
description: "Configure Sentry's Lambda function wrapper"
sidebar_order: 2
---

On this page you'll learn about the options to configure the `Sentry.wrapHandler` wrapper for your Lambda function.

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).

## Flush Timeout

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:

```javascript {filename:index.js} {2}
exports.handler = Sentry.wrapHandler(yourHandler, {
flushTimeout: 1000,
});
```

## Timeout Warning

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.

```javascript {filename:index.js} {2}
exports.handler = Sentry.wrapHandler(yourHandler, {
captureTimeoutWarning: false,
});
```

To change the timeout warning limit, assign a numeric value (in ms) to `timeoutWarningLimit`:

```javascript {filename:index.js} {2}
exports.handler = Sentry.wrapHandler(yourHandler, {
timeoutWarningLimit: 700,
});
```

## Capture Rejected Promises in `Promise.allSettled`

By default, Sentry captures errors raised by your handler.
However, your handler might return a `Promise.allSettled` result.
In this case, even if one of the messages has failed, Sentry won't capture any exception.

The `captureAllSettledReasons` (default: `false`) option captures all promise rejected results

```javascript {tabTitle:captureAllSettledReasons}
exports.handler = Sentry.wrapHandler(
() => {
return Promise.allSettled([
Promise.rejected(new Error("first")),
Promise.rejected(new Error("second")),
]);
},
{ captureAllSettledReasons: true }
);
// `first` and `second` errors are captured
```

This file was deleted.

118 changes: 18 additions & 100 deletions docs/platforms/javascript/guides/aws-lambda/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,115 +13,33 @@ Looking for instructions on how to add Sentry without modifying your code? [Chec

</Note>

Before you begin, note:
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.

- The estimated time to configure the integration is 5 - 15 minutes.
- You need to have experience with AWS console and a basic understanding of Lambda.
- 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).
## Installation

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

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

<OnboardingOptionButtons
options={["error-monitoring", "performance", "profiling"]}
/>
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).

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/).
## Configuration

<PlatformContent includePath="getting-started-install" />
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.

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

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

You can use the AWS Lambda integration for the Node like this:

<SignInNote />

```javascript {tabTitle:async}
const Sentry = require("@sentry/aws-serverless");

Sentry.init({
dsn: "___PUBLIC_DSN___",

// Add Tracing by setting tracesSampleRate and adding integration
// Set tracesSampleRate to 1.0 to capture 100% of transactions
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});

exports.handler = Sentry.wrapHandler(async (event, context) => {
// Your handler code
});
```javascript {filename:index.js}{tabTitle:CommonJS}{2}
exports.handler = async (event, context) => {
throw new Error("This is a test error");
};
```

```javascript {tabTitle:sync}
const Sentry = require("@sentry/aws-serverless");

Sentry.init({
dsn: "___PUBLIC_DSN___",

// We recommend adjusting this value in production, or using tracesSampler
// for finer control
tracesSampleRate: 1.0,
});

exports.handler = Sentry.wrapHandler((event, context, callback) => {
// Your handler code
});
```javascript {filename:index.mjs}{tabTitle:ESM}{2}
export const handler = async (event, context) => {
throw new Error("This is a test error");
};
```

{/* <!-- TODO-ADD-VERIFICATION-EXAMPLE --> */}

## Enable Timeout Warning

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`

```javascript {tabTitle:captureTimeoutWarning}
exports.handler = Sentry.wrapHandler(yourHandler, {
captureTimeoutWarning: false,
});
```

```javascript {tabTitle:timeoutWarning}
exports.handler = Sentry.wrapHandler(yourHandler, {
timeoutWarningLimit: 50,
});
```

## Capture Rejected Promises in `Promise.allSettled`

By default, Sentry captures errors raised by your handler.
However, your handler might return a `Promise.allSettled` result.
In this case, even if one of the messages has failed, Sentry won't capture any exception.

The `captureAllSettledReasons` (default: `false`) option captures all promise rejected results

```javascript {tabTitle:captureAllSettledReasons}
exports.handler = Sentry.wrapHandler(
() => {
return Promise.allSettled([
Promise.rejected(new Error("first")),
Promise.rejected(new Error("second")),
]);
},
{ captureAllSettledReasons: true }
);
// `first` and `second` errors are captured
```

## Behavior

With the AWS Lambda integration enabled, the Node SDK will:

- Automatically report all events from your Lambda Functions.
- Allows you to <PlatformLink to="/configuration/sampling/#configuring-the-transaction-sample-rate">modify the transaction sample rate</PlatformLink> using <PlatformIdentifier name="traces-sample-rate" />.
- Issue reports automatically include:
- A link to the cloudwatch logs
- Function details
- sys.argv for the function
- AWS Request ID
- Function execution time
- Function version
- 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
110 changes: 110 additions & 0 deletions docs/platforms/javascript/guides/aws-lambda/install/cjs-layer.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
title: Lambda Layer - CJS
description: "Learn how to add the Sentry Node Lambda Layer to use Sentry in your Lambda functions running in CommonJS (CJS)"
sidebar_order: 1
---

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).
If you follow this guide, you don't have to worry about deploying Sentry dependencies alongside your function code.
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.

<Note>

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).

</Note>

## 1. Prerequisites

Before you begin, make sure you have the following:

- You have a Lambda function that is running in CommonJS (CJS) mode, using `require` syntax.
- You know the AWS region that your function is deployed to.

## 2. Add the Sentry Lambda Layer

Add the Sentry Layer by navigating to your Lambda function. Select **Layers**, then **Add a Layer**.

![](./img/lambda_view.png)

**Specify an ARN** tab as illustrated:

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

Finally, set the region and copy the provided ARN value into the input.

<LambdaLayerDetail canonical="aws-layer:node" />

<br />

## 3. Initialize the SDK with Environment Variables

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.

<OnboardingOptionButtons options={["error-monitoring", "performance"]} />

Set the following environment variables in your Lambda function configuration:

```bash {"onboardingOptions": {"performance": "3"}}
NODE_OPTIONS="-r @sentry/aws-serverless/awslambda-auto"
SENTRY_DSN="___PUBLIC_DSN___"
SENTRY_TRACES_SAMPLE_RATE="1.0"
```

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

![](./img/cjs_env_vars.png)

## Alternative: Initialize the SDK in Code

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.
This way, you can customize the SDK setup further.
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.

Make sure you completed [step 1](#1-prerequisites) and [step 2](#2-add-the-sentry-lambda-layer) before proceeding.

```javascript {filename:index.js} {"onboardingOptions": {"performance": "5-8"}}
const Sentry = require("@sentry/aws-serverless");

Sentry.init({
dsn: "__PUBLIC_DSN__",
// Add Tracing by setting tracesSampleRate and adding integration
// Set tracesSampleRate to 1.0 to capture 100% of transactions
// We recommend adjusting this value in production
tracesSampleRate: 1.0,
});

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

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.

That's it - you're all set!

## Lambda layer for v7 SDK

The instructions above are written for SDK version 8 (the most recent version).
You can also install a v7 version of the Sentry Lambda layer in case you can't upgrade to v8.
The procedure is identical to the instructions above except for two differences:

### v7 layer ARN

The v7 Lambda layer has a different ARN:

```
arn:aws:Lambda:us-west-1:943013980633:layer:SentryNodeServerlessSDKv7:3
```

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`:

### v7 package name

The `@sentry/aws-serverless` package was called `@sentry/serverless` prior to version 8. Therefore, for the v7 layer, adjust your `NODE_OPTIONS` environment variable:

```bash
NODE_OPTIONS="-r @sentry/serverless/dist/awslambda-auto"
```

The other environment variables remain the same as above.
Loading

0 comments on commit f13e2f6

Please sign in to comment.