The easiest way to get started with Sentry is to use the Sentry Lambda Layer instead of installing @sentry/aws-serverless
with a package manager manually. If you follow this guide, you don't have to worry about deploying Sentry dependencies alongside your function code.
Before you begin, make sure you have the following:
Add the Sentry Layer by navigating to your Lambda function. Select Layers, then Add a Layer.
Specify an ARN tab as illustrated:
Finally, set the region and copy the provided ARN value into the input.
Choose your setup method based on your Lambda function type:
The setup instructions you should follow depend on how your 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.
CommonJS 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.
In addition to capturing errors, you can monitor interactions between multiple services or applications by enabling tracing.
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
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) => {
});
Option B: Manual Setup
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.
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.
For CommonJS Lambda Functionsindex.js
Copied
const Sentry = require("@sentry/aws-serverless");
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
sendDefaultPii: true,
tracesSampleRate: 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";
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
sendDefaultPii: true,
tracesSampleRate: 1.0,
});
Load the SDK
To load the SDK before your function starts, you need to preload the instrument.mjs
by setting the NODE_OPTIONS
environment variable:
Copied
NODE_OPTIONS="--import ./instrument.mjs"
That's it — make sure to re-deploy your function and you're all set!
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