NPM package - CJS
Learn how to set up Sentry manually for Lambda functions running in CommonJS (CJS) 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 CommonJS (CJS) mode. This installation method allows you to fully customize your Sentry SDK setup while also being able to tree-shake, transpile and bundle the SDK 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, you might want to use the Lambda Layer instead.
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're able to deploy dependencies (i.e.
node_modules
) alongside your function code to AWS Lambda.
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.
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
Initialize and configure the SDK in your Lambda function Code. Call Sentry.init
outside of the handler function to avoid re-initializing the SDK on every invocation. Also, add the Sentry.wrapHandler
wrapper around your function to automatically catch errors and performance data:
index.js
const Sentry = require("@sentry/aws-serverless");
const { nodeProfilingIntegration } = require("@sentry/profiling-node");
Sentry.init({
dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
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,
});
exports.handler = Sentry.wrapHandler(async (event, context) => {
// Your handler code
});
That's it - make sure to re-deploy your function and you're all set!
The instructions above are written for SDK version 8 (the most recent version). 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
.
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").