4

Can you please advise, if there are any issues with using of FastAPI Web Framework in AWS Lambda:

  • What framework capabilities will be lost if using in AWS Lambda environment?
  • Is there any overhead associated with ASGI technology or anything else, e.g. when we have to use additional libraries like Magnum?

2 Answers 2

6

We have been using the API gateway -> AWS Lambda -> Mangum -> FastApi combo in production for 3-4 months now and have had no issues.

The restrictions come from the fact that Lambda is a sync request/reply setup.

  • No point doing anything async in your code - AWS lambda will create a container/python process for each request.
  • No response streaming bodies. (use s3 for files etc)

The overhead is tiny, Mangum is super light ( see how little code is in: https://github.com/jordaneremieff/mangum/tree/main/mangum) its basically just restructuring the input dictionary.

6
  • Many thanks, Nath... ok I can understand "no async" when processing requests as Lambda handles a single request, but why not use async when you connect to the other aws resources or databases as a part of the request request processing?
    – Viji
    Aug 18, 2022 at 10:02
  • 2
    yep you are correct, You can't parallelize processing of requests but could use async for parallelizing access to resources required to complete a single request.
    – Nath
    Aug 19, 2022 at 2:22
  • how about the logs? What method are u using to group logs from the endpoints? May 4 at 12:24
  • for logs we are using the. awslabs.github.io/aws-lambda-powertools-python/2.15.0/core/… library and setting the POWERTOOLS_SERVICE_NAME to group logs from various different lambdas.
    – Nath
    May 9 at 2:14
  • That's not quite true of lambda. What is true is that the default python lambda runtimes provided by AWS all poll AWS lambda's backend for events API and invoke your handler in a synchronous loop. You could write your own runtime with an asynchronous event loop under the hood. Other runtimes (IE node, go) have asynchronous loops by default. May 16 at 15:29
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.