Skip to content
tezvyn:
Menu

Mangum: Run Python ASGI Apps on Serverless

Source: mangum.ioHardHow cards are made

Mangum: Run Python ASGI Apps on Serverless

Mangum is an adapter for running Python ASGI apps (like FastAPI) on serverless platforms like AWS Lambda. It translates serverless events into ASGI requests, letting you deploy existing async web apps without a rewrite.

Why it exists

Modern Python web frameworks like FastAPI are built on the ASGI specification, designed for async servers. Serverless platforms like AWS Lambda have their own event-driven model. A bridge is needed to connect these two different worlds without rewriting the application to fit a platform-specific handler.

The mental model

Think of Mangum as a universal adapter for your ASGI application. Your app is a device with a standard ASGI plug. A traditional server like Uvicorn is a standard ASGI wall socket. A serverless platform like AWS Lambda is a completely different type of socket. Mangum is the travel adapter that lets you plug your ASGI device into the Lambda socket, translating the event payloads so your application works without modification.

How it works

When a serverless function is invoked by an event, like an API Gateway request, the platform passes an event payload to the handler. Mangum takes this payload, which contains all the HTTP request details, and transforms it into the standard ASGI 'scope' dictionary. It passes this scope to your ASGI application. Your app processes the request and returns an ASGI response, which Mangum then translates back into the format required by the serverless platform to send back to the client.

When to use it

Use Mangum when you want to deploy an ASGI-compliant application (built with FastAPI, Starlette, etc.) to a serverless environment like AWS Lambda. It's perfect for building microservices or APIs where you want the benefits of serverless (auto-scaling, pay-per-use) without giving up the development experience of modern Python web frameworks.

When not to use it

Avoid Mangum if your application relies heavily on features that are at odds with the serverless execution model. This includes long-lived WebSocket connections, in-memory state that must persist between requests, or background tasks that exceed the function's maximum execution time. A traditional server is a better fit for these use cases.

One canonical example

A developer builds a REST API using FastAPI. Instead of provisioning a server, they wrap the FastAPI application object with Mangum and deploy it as an AWS Lambda function fronted by an API Gateway. Each API call invokes the Lambda, Mangum translates the request, FastAPI processes it, and the response is returned, scaling automatically without server management.

Interview question

Which characteristic of an ASGI application would make it LEAST suitable for deployment using Mangum on a serverless platform?

  • a.It performs CPU-intensive computations that can complete within the function's execution limit.
  • b.It requires persistent in-memory state to be maintained between successive requests.Correct
  • c.It is built using an asynchronous framework like Starlette.
  • d.It processes standard HTTP GET and POST requests.
Why?

The card explicitly states that Mangum should be avoided if an application relies on "in-memory state that must persist between requests," as this conflicts with the ephemeral nature of serverless functions. The other options describe scenarios that are well-suited for Mangum and serverless deployment.

Just read this? Test yourself on what you have been reading.

Read the original → mangum.io

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.

See open roles