Awesome Open Source
Awesome Open Source

Mangum

Package version Build Status PyPI - Python Version

Mangum is an adapter for using ASGI applications with AWS Lambda & API Gateway. It is intended to provide an easy-to-use, configurable wrapper for any ASGI application deployed in an AWS Lambda function to handle API Gateway requests and responses.

Documentation: https://mangum.io/

Features

  • API Gateway support for HTTP, REST, and WebSocket APIs.

  • Multiple storage backend interfaces for managing WebSocket connections.

  • Compatibility with ASGI application frameworks, such as Starlette, FastAPI, and Quart.

  • Support for binary media types and payload compression in API Gateway.

  • Works with existing deployment and configuration tools, including Serverless Framework and AWS SAM.

  • Startup and shutdown lifespan events.

Requirements

Python 3.6+

Installation

pip install mangum

Example

from mangum import Mangum

async def app(scope, receive, send):
    await send(
        {
            "type": "http.response.start",
            "status": 200,
            "headers": [[b"content-type", b"text/plain; charset=utf-8"]],
        }
    )
    await send({"type": "http.response.body", "body": b"Hello, world!"})


handler = Mangum(app)

or using a framework:

from mangum import Mangum
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette.routing import Route


async def homepage(request):
    response = PlainTextResponse("Hello, world!")

    return response


app = Starlette(debug=True, routes=[Route("/", homepage)])

handler = Mangum(app)

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Python (1,124,651
Django (12,454
Aws (10,952
Serverless (4,691
Aws Lambda (2,993
Lambda (2,245
Asyncio (1,596
Api Gateway (1,011
Fastapi (872
Sanic (219
Asgi (167
Starlette (144
Related Projects