ASGI serverless adapters
Project description
Mangum
Documentation: https://erm.github.io/mangum/
Mangum is a library for using ASGI applications with FaaS platforms.
Important: This project is under active development and in an experimental/unstable state.
Requirements
Python 3.6+
Installation
pip3 install mangum
Dependencies
Currently there are two optional dependencies.
- azure-functions - Required for
azure_handler
. - boto3 - Required for the
mangum
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 that can be used with the handler methods:
from mangum.adapters.aws 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 aws_handler(App, event, context)
Mangum CLI (experimental)
Experimental AWS packaging/deployment support to generate, package, and deploy an application. The generated application and template only provide basic functionality.
Requirements:
- AWS CLI & credentials
- Python 3
To quickly generate an app via the command-line, you may use the following command:
mangum init
This generates the following:
-
A boilerplate ASGI app with the
mangum
package installed for deployment to AWS. -
A
settings.json
file with the generated AWS resource information. -
An S3 bucket to be used with the app.
-
A
template.yaml
SAM template.
Once generated you'll have an app structure that looks like this:
├── README.md
├── hello_asgi
│ ├── app.py
│ ├── mangum
│ ├── mangum-0.3.0-py3.7.egg-info
│ ├── requirements.txt
│ └── template.yaml
└── settings.json
You will then be prompted to enter the following commands, these are simply wrappers around the AWS-CLI commands with the settings.json
values generated previously as arguments.
magnum package
After packaging, you then can deploy:
mangum deploy
To add additional requiremets,
Azure Functions
Example
The same example application above may be used with Azure Functions:
from mangum.adapters.azure 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(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.
Source Distribution
File details
Details for the file mangum-0.4.7.tar.gz
.
File metadata
- Download URL: mangum-0.4.7.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8918792b48197e653edba6c702c4a8ade497b54dc8fdba6ca13c8a540fe9e69 |
|
MD5 | d1a893445b49e26bca153759553f31f3 |
|
BLAKE2b-256 | 4442c30e7468fdbd3312afa25e555206285e744cde3a8ab7db5bcc15101fabec |