NPM Package - ESM
Learn how to set up Sentry manually for Lambda functions running in EcmaScript Modules (ESM) using Sentry's AWS Serverless SDK NPM package
In this guide you will learn how to set up the @sentry/aws-serverless
SDK for AWS Lambda functions running in EcmaScript Modules (ESM) mode. 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 in code.
At this time, this guide shows the only way to use Sentry with ESM lambda functions. You cannot use the Sentry AWS Lambda Layer with ESM. We're working on an ESM lambda layer to provide a simpler setup but need to solve several OpenTelemetry-related limitations first.
Before you begin, make sure you have the following:
- You have a lambda function that is running in EcmaScript Modules (ESM) mode, using
import
syntax. - You're able to deploy dependencies (i.e.
node_modules
) alongside your function code to AWS Lambda.
Install the @sentry/aws-serverless
SDK using a package manager of your choice:
npm install @sentry/aws-serverless
npm install @sentry/aws-serverless @sentry/profiling-node
Add the Sentry.wrapHandler
wrapper around your function handler to automatically catch errors and performance data:
index.mjs
import * as Sentry from "@sentry/aws-serverless";
export const handler = Sentry.wrapHandler(async (event, context) => {
// Your handler code
});
To initialize and configure the SDK, you need to set the following environment variables in your AWS Lambda function configuration:
NODE_OPTIONS="--import @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:
That's it - make sure to re-deploy your function and you're all set!
To further customize the SDK setup, you can also manually initialize the SDK in your lambda function. Due to ESM limitations, you need to initialize the SDK in a separate file and load it before your function starts. The benefit of this installation method is that you can fully customize your Sentry SDK setup in a Sentry.init
call.
Follow steps 1 and 2 above to install the SDK NPM package in your project.
Create a new file, for example instrument.mjs
to initialize the SDK:
instrument.mjs
import * as Sentry from "@sentry/aws-serverless";
import { nodeProfilingIntegration } from "@sentry/profiling-node";
Sentry.init({
dsn: "__PUBLIC_DSN__",
integrations: [nodeProfilingIntegration()],
// 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,
// Set sampling rate for profiling - this is relative to tracesSampleRate
profilesSampleRate: 1.0,
});
Add the Sentry.wrapHandler
wrapper around your function handler to automatically catch errors and performance data:
index.mjs
import * as Sentry from "@sentry/aws-serverless";
export const handler = Sentry.wrapHandler(async (event, context) => {
// Your handler code
});
To load the SDK before your function starts, you need to preload the instrumentation.mjs
by setting the NODE_OPTIONS
environment variable:
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!
The v7 @sentry/serverless
SDK does not work correctly with ESM-based Lambda functions. Please upgrade to the v8 SDK and follow the instructions above.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").