ASGI serverless adapters
Project description
Mangum
Documentation: https://erm.github.io/mangum/
Mangum is a library for using ASGI applications with FaaS platforms.
Requirements
Python 3.6+
Installation
$ pip3 install mangum
Dependencies
Currently there are two optional dependencies.
- azure-functions - Required for Azure.
- boto3 - Required for the AWS CLI commands.
This can be installed with:
$ pip3 install mangum[full]
Supported Platforms
Only two platforms are currently supported, but if you'd like to see others, please open an issue.
AWS Lambda / API Gateway
Example
Below is a basic ASGI application example using the AWS run method:
from mangum.platforms.aws.adapter import run_asgi
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 run_asgi(App, event, context)
As a middleware
The same application above can be run using the AWSLambdaMiddleware
. Currently it just implements a class that returns the run_asgi
response.
from mangum.platforms.aws.middleware import AWSLambdaMiddleware
def lambda_handler(event, context):
return AWSLambdaMiddleware(app)
Mangum CLI (experimental)
Experimental AWS packaging/deployment support. This requires installation of the optional dependencies for AWS:
$ pip install mangum[full]
It also requires:
- AWS CLI
- AWS credentials.
The available commands are briefly outlined below, but there is also a quickstart guide here:
-
mangum aws init
- Create a new configuration template for an application. -
mangum aws build
- Install the requirements and copy the application files into the build directory. -
mangum aws package
- Package the local project to prepare for deployment. -
mangum aws deploy
- Deploy the packaged application to AWS. -
mangum aws tail
- Tail the last 10 minutes of CloudWatch for the function. -
mangum aws describe
- Retrieve the API endpoints for the function. -
mangum aws validate
- Validate the SAM template in the current configuration.
Azure Functions
Example
The following is an example of using the Azure Function adapter method:
from mangum.platforms.azure.adapter import run_asgi
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 main(req):
return run_asgi(App, req)
The command-line tools for Azure Functions can do pretty much everything you need. A basic quickstart guide for using it with Mangum is outlined here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.