In this guide you will learn how to set up the @sentry/aws-serverless
SDK for AWS Lambda functions using NPM. We recommend starting the SDK automatically via environment variables so that you only have to make minimal code changes to your lambda function. If you need more control over the SDK setup, you can also initialize the SDK in 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, use the Lambda Layer instead.
1. PrerequisitesBefore you begin, make sure you have the following:
node_modules
) alongside your function code to AWS Lambda.The setup instructions you should follow depend on how your Lambda function runs at runtime, not how it's written in your source code.
require()
and module.exports
at runtime)import
/export
at runtime)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.
In addition to capturing errors, you can monitor interactions between multiple services or applications by enabling tracing. You can also collect and analyze performance profiles from real users with profiling.
Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below.
Error Monitoring Tracing Profiling
Install the @sentry/aws-serverless
SDK using a package manager of your choice:
Copied
npm install @sentry/aws-serverless @sentry/profiling-node
3. Setup Options
Choose your setup method based on your Lambda function type:
Option A: Automatic SetupCommonJS functions support fully automatic setup using environment variables - both SDK initialization and handler wrapping are handled automatically.
ESM functions support automatic SDK initialization via environment variables, but require manual handler wrapping.
Set the following environment variables in your Lambda function configuration:
Copied
NODE_OPTIONS="--require @sentry/aws-serverless/awslambda-auto"
SENTRY_DSN="https://examplePublicKey@o0.ingest.sentry.io/0"
SENTRY_TRACES_SAMPLE_RATE="1.0"
To set environment variables, navigate to your Lambda function, select Configuration, then Environment variables:
You'll also need to manually wrap your handler as shown below:
index.mjs
Copied
import * as Sentry from "@sentry/aws-serverless";
export const handler = Sentry.wrapHandler(async (event, context) => {
});
That's it - make sure to re-deploy your function and you're all set!
Option B: Manual SetupTo 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.
You can initialize the SDK directly in your main handler file:
index.js
Copied
const Sentry = require("@sentry/aws-serverless");
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
sendDefaultPii: true,
integrations: [nodeProfilingIntegration()],
tracesSampleRate: 1.0,
profilesSampleRate: 1.0,
});
exports.handler = Sentry.wrapHandler(async (event, context) => {
});
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.
First, wrap your handler:
index.mjs
Copied
import * as Sentry from "@sentry/aws-serverless";
export const handler = Sentry.wrapHandler(async (event, context) => {
});
Due to ESM limitations, you need to initialize the SDK in a separate file and load it before your function starts.
Create a new file, for example instrument.mjs
to initialize the SDK:
instrument.mjs
Copied
import * as Sentry from "@sentry/aws-serverless";
import { nodeProfilingIntegration } from "@sentry/profiling-node";
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
sendDefaultPii: true,
integrations: [nodeProfilingIntegration()],
tracesSampleRate: 1.0,
profilesSampleRate: 1.0,
});
Load the SDK
To load the SDK before your function starts, you need to set the NODE_OPTIONS
environment variable:
Copied
NODE_OPTIONS="--import ./instrument.mjs"
To set environment variables, navigate to your Lambda function, select Configuration, then Environment variables.
That's it - make sure to re-deploy your function and you're all set!
Using the v7 SDKThe instructions above are written for the latest SDK version.
For CommonJS functions: 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
.
For ESM functions: 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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4