Short answer, not really. There is a tiny, tiny bit of overhead to translate the incoming event into an ASGI event and instantiate an ASGI-callable object(that's what frameworks like mangum do), but it is pretty negligable.
There are many good reasons to use FastAPI over something like flask or even just a basic lambda REST api. My biggest reason is the integration with pydantic BaseModel
, it removes the need for lots of validation code.
Taking advantage of concurrency via an async
handler function is not one of those advantages, at least in the default python lambda runtimes.
The main reason is that the runtime itself is essentially a SYNCHONOUS loop which polls their API for events and then invokes your handler.
The actual loop pulled directly from the AWS lambda runtime for python3.7 a few years ago:
while True:
event_request = lambda_runtime_client.wait_next_invocation()
_GLOBAL_AWS_REQUEST_ID = event_request.invoke_id
update_xray_env_variable(event_request.x_amzn_trace_id)
handle_event_request(lambda_runtime_client,
request_handler,
event_request.invoke_id,
event_request.event_body,
event_request.content_type,
event_request.client_context,
event_request.cognito_identity,
event_request.invoked_function_arn,
event_request.deadline_time_in_ms,
log_sink)
Unless you write your own custom python runtime with an asynchronous event loop, making your handler function async
will have zero effect because the above loop will not poll with wait_next_invocation
until handle_event_request
is complete. I know that Go
and Node
default runtimes have an asynchronous base loop, so having an async
handler is actually legit there. Others could too, I just don't know and it is not well documented.