I was wondering why AWS Lambda always needs a handler on top of the ASGI server.
When I deploy a FastAPI application on Cloud Run, I dockerize it, push it, and it works 1:1 to my local machine.
Lambda feels more “alien” than Cloud Run.
I wanted to understand why.
AWS Lambda uses an event-driven architecture by default. Each function invocation is triggered by an event. The handler is an adapter between Lambda’s event-based system and whatever web framework you are using.
The handler receives and event object and context and returns a response.
So when you want to listen on a port, you need to create a CloudBridge that creates an event and passes it to the handler.
For contrast, Cloud Run runs containers that can directly listen on a port.
Cloud Run is a service. AWS Lambda is a consumer.
I prefer the Cloud Run approach. It takes more steps when I want to trigger it with another event (e.g. insertion into a database or a bucket), but it’s closer to the local machine, so I can test it more easily.