AWS Lambda Layer

This guide walks you through how to add Sentry to your AWS Lambda function by adding the Sentry AWS Lambda layer. This method can be instrumented from the Sentry product by those who have access to the AWS infrastructure and doesn't require that you make any direct updates to the code.

You can also configure this function in one of the two ways described below:

  • Without touching your code: This method can be instrumented from the Sentry product by those who have access to the AWS infrastructure and doesn't require that you make any direct updates to the code. See the AWS Lambda guide.
  • By adding the Sentry Lambda Layer to your function manually: While this is a quick way to add Sentry to your AWS Lambda function, it gives you limited configuration possibilities with environment vars. See AWS Lambda Layer.

Navigate to your Lambda function:

Scroll down to the Layers section and click Add a Layer:

Specify an ARN tab as illustrated:

First, set the region and copy the provided ARN value:

Select Region

Now copy that ARN into the input box:

For Sentry to instrument your Lambda function, you need to change the handler of your function and set the Sentry DSN using environment variables.

The Sentry layer will wrap your function handler to initialise the Sentry SDK in your function.

In you Lambda function, scroll to Runtime settings and click Edit:

Note your current Handler and then change it to: sentry_sdk.integrations.init_serverless_sdk.sentry_lambda_handler

After saving, open the "Configuration" tab of your Lambda Function and set SENTRY_INITIAL_HANDLER to the handler you noted before:

Make sure that SENTRY_DSN and SENTRY_INITIAL_HANDLER are set in your environment at minimum. See "Set the Sentry Handler" above for more information.

You can configure Sentry by setting these environment variables for your Lambda function:

  • SENTRY_INITIAL_HANDLER: The handler function of your AWS Lambda funtion.
  • SENTRY_DSN: This is set to the DSN of your project
  • SENTRY_TRACES_SAMPLE_RATE: This sets the sampling rate for transactions.

Add an error to your function and run it. If everything's working properly, it should be captured and sent to Sentry.io.

  • Error: "Unable to import module 'lambda_function': urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0.2k-fips 26 Jan 2017'. See: https://github.com/urllib3/urllib3/issues/2168"

    This error means that you have urllib3>=2.0 in you dependencies. The Python runtime used in AWS Lambda is not compatible with urllib3 version 2, because it still uses OpenSSL 1.0.

    Solution: There are two solutions for this error:

    • Either use a newer Python runtime for your Lambda function. urllib3 version 2+ is supported in AWS Lambda from the Python 3.9 Lambda runtime and newer.
    • Or pin your dependency of urllib3 to urllib3<1.27 to make it work with the Python runtime 3.7 and 3.8 used in AWS Lambda.

  • Lambda Layer up to Version 63: Python 3.6, 3.7, 3.8, 3.9
  • Lambda Layer Version 64+: Python 3.9, 3.10, 3.11
  • Lambda Layer Version 77+: Python 3.7, 3.8, 3.9, 3.10, 3.11
Help improve this content
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").