Skip to main content

sentry-lwa

Drop-in AwsLambdaIntegration for ASGI apps on AWS Lambda behind the Lambda Web Adapter.

by Weird Sheep Labs

Why this exists

The Lambda Web Adapter ships its own Rust-based runtime interface client (the lambda_http crate) and uses it to poll the Lambda Runtime API directly. Your ASGI app runs as a plain HTTP server behind it — awslambdaric is never imported.

Sentry's native AwsLambdaIntegration monkeypatches awslambdaric's bootstrap module. Under the Web Adapter, setup_once() finds nothing to patch and returns early. The flush hook, timeout warning, and Lambda context enrichment are all inert. Lambda freezes the execution environment as soon as the response is sent, and any Sentry spans still queued at that point are silently lost.

This is a known issue. Others have reported identical data loss with DataDog and OpenTelemetry:

  • #466 — DataDog traces lost because Lambda freezes before post-response middleware completes
  • #404 — maintainer confirms background tasks are killed on freeze, suggests the internal extension pattern as a workaround
  • #590 — the official fastapi-background-tasks extension example does not work (still open)

What this does

sentry-lwa provides a single AwsLambdaIntegration that replaces the native one. It:

  1. Registers an internal Lambda extension that delays the freeze by holding off its Next call until the Sentry transport queue is flushed.
  2. Wraps Starlette.__call__ to bridge the ASGI request lifecycle to the extension thread.
  3. Enriches every Sentry event with Lambda metadata (function name, ARN, CloudWatch log link, execution duration) from the x-amzn-lambda-context header.
  4. Optionally fires a ServerlessTimeoutWarning ~1.5 s before the invocation deadline.

Works with any ASGI framework that runs on Starlette — FastAPI, MCP servers via fastmcp, plain Starlette apps.

Installation

pip install sentry-lwa

Usage

Swap one import. Use whatever native Sentry integration your framework needs — AwsLambdaIntegration handles everything that breaks under the Web Adapter.

FastAPI

import sentry_sdk
from sentry_sdk.integrations.fastapi import FastApiIntegration
from sentry_lwa import AwsLambdaIntegration

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

MCP server (fastmcp)

import sentry_sdk
from sentry_sdk.integrations.mcp import MCPIntegration
from sentry_lwa import AwsLambdaIntegration

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

Parameters

Parameter Type Default Description
timeout_warning bool False Fire a ServerlessTimeoutWarning ~1.5 s before the invocation deadline

Behaviour

  • Inside Lambda — the extension registers with the Extensions API, waits for each request to finish, flushes the Sentry queue (up to 2 s, capped by the invocation deadline), then calls Next to release the freeze. Every Sentry event is enriched with Lambda metadata.
  • Outside Lambda (AWS_LAMBDA_RUNTIME_API not set) — the extension does not register and every request passes through unchanged. No overhead.

How it works

  1. AwsLambdaIntegration subclasses the native AwsLambdaIntegration but does not call super().setup_once() (it would no-op under the Web Adapter).
  2. setup_once() wraps Starlette.__call__ with a layer that parses the x-amzn-lambda-context header, adds a per-request event processor to the Sentry scope, optionally starts a timeout warning thread, signals the extension thread when the request starts and finishes.
  3. setup_once_with_options() registers a SentryFlushExtension — an internal Lambda extension running in a background thread.
  4. The extension thread (blocked on the INVOKE event from the Extensions API) wakes when the ASGI thread signals done, calls sentry_sdk.flush(), then calls Next — allowing Lambda to freeze.

Metadata

Release files for sentry-lwa 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sentry-lwa 1.0.0
File Size Uploaded Provenance Actions
sentry_lwa-1.0.0.tar.gz 10.6 kB Without provenance Details for sentry_lwa-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sentry-lwa 1.0.0
File Interpreter ABI Platform Actions
sentry_lwa-1.0.0-py3-none-any.whl Python 3 none any Details for sentry_lwa-1.0.0-py3-none-any.whl

Total release size: 22.6 kB

Release files / sentry_lwa-1.0.0.tar.gz

Download URL sentry_lwa-1.0.0.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d05145e9075d98d06560f728db9312d273f6755f0bd374de60978d67287f0767
BLAKE2b-256 checksum
How to use checksums
83510b702099db99dda54884157a408e4a87d7c62f715fac1155ea2c0fabd451
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / sentry_lwa-1.0.0-py3-none-any.whl

Download URL sentry_lwa-1.0.0-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3991f1cb3abaec5d7a9aa675c56da47e37ba1c2e6b68e3bf88cab75d04eb9953
BLAKE2b-256 checksum
How to use checksums
d01a77f18d114f1f21ef63f5d17de47862c862f620150494b05aa87fb38b2d99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for sentry-lwa 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page