AWS Lambda Manual Instrumentation

Learn about instrumenting your AWS lambda function manually using Sentry.

This guide walks you through how to manually add Sentry to your AWS Lambda function by installing the Sentry SDK into your AWS Lambda function packages. While this method takes more effort, it gives you full control of your Sentry setup and manual instrumentation.

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.

Create a deployment package on your local machine and install the required dependencies in the deployment package. For more information, see AWS Lambda deployment package in Python.

Install our Python SDK into your package directory using pip:

Copied
pip install --target ./package --upgrade sentry-sdk

Here's how to use the AWS Lambda integration for the Python SDK:

Copied
import sentry_sdk
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    traces_sample_rate=1.0,
    # Set profiles_sample_rate to 1.0 to profile 100%
    # of sampled transactions.
    # We recommend adjusting this value in production.
    profiles_sample_rate=1.0,
    integrations=[
        AwsLambdaIntegration(),
    ],
)

def lambda_handler(event, context):
    # ...

Check out Sentry's AWS sample apps for detailed examples.

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

With the AWS Lambda integration enabled, the Python SDK will:

  • Automatically report all events from your Lambda Functions.

  • Allow you to modify the transaction sample rate using traces_sample_rate.

  • Automatically include the following event reports:

    • A link to CloudWatch Logs
    • Function details
    • sys.argv for the function
    • AWS Request ID
    • Function execution time
    • Function version

    You can add more data as described here.

  • Request data including HTTP method, URL, headers, form data, and JSON payloads will be attached to all events. Sentry excludes raw bodies and multipart file uploads. Sentry also excludes personally identifiable information such as user ids, usernames, cookies, authorization headers, and IP addresses (unless you set send_default_pii to True).

  • Each request has a separate scope. Changes to the scope within a view, for example setting a tag, will only apply to events sent as part of the request being handled.

You can change the behavior of AwsLambdaIntegration by setting options for it:

  • timeout_warning:

    The timeout warning reports an issue when the function execution time is near the configured timeout.

    To enable the warning, update the SDK initialization to set timeout_warning to True:

    Copied
    sentry_sdk.init(
        # ...
        integrations=[
            AwsLambdaIntegration(timeout_warning=True),
        ],
    )
    

    The timeout warning is sent only if the timeout in the Lambda function configuration is set to a value greater than one second.

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").