Skip to main content

ASGI to AWS Lambda adapter

Project description

mangum

An attempt to provide simple AWS Lambda/API Gateway support to any ASGI application.

Work in progress.

Examples

Plain ASGI

Below is a basic ASGI application that returns a "hello world" response:

from mangum import asgi_response


class App:
    def __init__(self, scope) -> None:
        self.scope = scope

    async def __call__(self, receive, send) -> None:
        message = await receive()
        if message["type"] == "http.request":
            await send(
                {
                    "type": "http.response.start",
                    "status": 200,
                    "headers": [[b"content-type", b"text/plain"]],
                }
            )
            await send({"type": "http.response.body", "body": b"Hello, world!"})


def lambda_handler(event, context):
    return asgi_response(App, event, context)

Starlette

Here is another example, this time using Starlette, to demonstrate that the response method can be used with frameworks as well:

from mangum import asgi_response
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse

app = Starlette()

@app.route("/")
def homepage(request):
    return PlainTextResponse("Hello, world!")

def lambda_handler(event, context):
    return asgi_response(app, event, context)

Todo

  • WebSocket support through API Gateway
  • Chunked responses/streaming
  • More tests
  • Detailed instructions
  • More framework examples
  • Lots

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mangum-0.0.4.tar.gz (3.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page