Mangum is an adapter designed to run ASGI (Asynchronous Server Gateway Interface) applications, such as FastAPI, Starlette, Quart, and Django, within AWS Lambda environments. It handles various AWS event types including Function URLs, API Gateway (HTTP and REST APIs), Application Load Balancer, and CloudFront Lambda@Edge. The library is actively maintained, with frequent releases, and is currently at version 0.21.0.
pip install mangumVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to wrap a simple FastAPI application with Mangum to make it deployable as an AWS Lambda function. The `handler` object is the entry point for AWS Lambda, which Mangum uses to convert Lambda events into ASGI requests for your application.
Upgrade your Python runtime to 3.9 or later or pin your Mangum dependency to `<0.20.0`.
Initialize Mangum with `handler = Mangum(app, api_gateway_base_path="/your/stage/path")` and configure your FastAPI app with `app = FastAPI(root_path="/your/stage/path")` as needed.
Set `lifespan="on"` for explicit lifespan management or `lifespan="auto"` if your application framework supports it and you want Mangum to automatically handle it.
Monitor Mangum's GitHub issues and releases for updates regarding asyncio event loop management and potential migration steps.
Consult Mangum's documentation on `text_mime_types` and `exclude_headers` parameters to correctly configure how different content types are processed.
Ensure your API Gateway is configured with a proxy integration (`/{proxy+}`) and that the Mangum handler is initialized with `api_gateway_base_path` if your API Gateway has a base path. Also, set the `root_path` when creating your FastAPI app and consider adding routes for both trailing and non-trailing slashes.When testing locally, ensure your test event payload mimics a valid AWS event type (e.g., API Gateway HTTP API v2.0 event). When deploying, verify that the Lambda function's trigger (e.g., API Gateway, Function URL) is sending a compatible event structure to Mangum. Ensure your `lambda_handler` in `main.py` (or similar) is correctly defined as `handler = Mangum(app)`.
Ensure that your Lambda function is invoked with a full proxy integration from API Gateway, which provides the necessary `requestContext` in the event payload. If invoking directly, provide a mock event that includes a `requestContext` dictionary with relevant fields.
Verify that your Python application file (e.g., `app.py` or `main.py`) is at the root of your deployment package, or that your handler path (e.g., `app.handler` or `main.lambda_handler`) correctly reflects your file and handler function name. Ensure all necessary dependencies (including `mangum`) are included in your deployment package.