AWS Lambda Manual Instrumentation
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
:
pip install --target ./package --upgrade sentry-sdk
Here's how to use the AWS Lambda integration for the Python SDK:
import sentry_sdk
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
integrations=[
AwsLambdaIntegration(),
],
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production,
traces_sample_rate=1.0,
)
def lambda_handler(event, context):
# ...
Check out Sentry's AWS sample apps for detailed examples.
Note
If you're using another web framework inside of AWS Lambda, that framework may catch exceptions before we see them. Make sure to enable the framework-specific integration if it exists. See the Python main page for more information.
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
toTrue
).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
toTrue
:Copiedsentry_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.
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").
- Package:
- pypi:sentry-sdk
- Version:
- 2.0.0
- Repository:
- https://github.com/getsentry/sentry-python
- API Documentation:
- https://getsentry.github.io/sentry-python/