Deploying FastAPI as Lambda Function #221
Replies: 17 comments 7 replies
-
@chgangaraju the previous from fastapi import FastAPI
from mangum import Mangum
app = FastAPI()
@app.get("/")
def hello_world():
return {"hello": "world"}
handler = Mangum(app, spec_version=2) |
Beta Was this translation helpful? Give feedback.
-
Thanks, @ERM. It is working like a charm. It will be very helpful if this is updated in docs and examples. |
Beta Was this translation helpful? Give feedback.
-
@chgangaraju Can you share your serverless.yml function setup? I want to know how you have organized the routing for FastAPI to handle and how does your project structure look like. I'm having a hard time figuring out doing it. |
Beta Was this translation helpful? Give feedback.
-
serverless.yml
app/main.py
Make sure that you are using a compatible python version. For example, If you are using Python 3.6, use mangum==0.6.6. As the latest version only works with Python 3.7+. |
Beta Was this translation helpful? Give feedback.
-
@chgangaraju what are the reasons you are using Python 3.6/Mangum 0.6.6? You brought up 3.6 support in a previous issue #33, and I'm still open to a potential refactor to support 3.6 again if there are specific reasons to do so. Also, FastAPI currently supports for ASGI 3, so I dropped |
Beta Was this translation helpful? Give feedback.
-
@ERM We have multiple projects and we are using Python 3.6 and flask, connexion in almost all projects. We experimented to use FastAPI& Mangum for one lambda. Even though it's easy to switch python versions with tools like |
Beta Was this translation helpful? Give feedback.
-
@anil-grexit, Refer to this working example @ERM, It's very hard to find a working example with the latest versions. I tried this example, but getting the following error.
|
Beta Was this translation helpful? Give feedback.
-
@chgangaraju The examples in that repo may be out-of-date (will update or archive it at some point), but I don't see anything obviously wrong with the application code that would produce that error with the latest versions. The main difficulty around creating examples has been that there are several different configuration/deployment methods which I am now treating as a separate concern from the adapter class itself. Initially I bundled a custom CLI for generating CF templates that I've since pulled out so some of the existing example code may not reflect this yet. Thanks for putting together your serverless repo. I'll take a look into what issues there may be between the latest version of the adapter against your example when I have a chance. |
Beta Was this translation helpful? Give feedback.
-
@chgangaraju I've tested against your example repo. If the requirements are changed to use the latest versions of FastAPI and Mangum with Python 3.7 there are no issues. I think there may be problems when using versions of FastAPI that do not support ASGI 3 that are causing the error above, but I was unable to reproduce your specific error. |
Beta Was this translation helpful? Give feedback.
-
I think the issue still persists while deploying FastAPI, using Mangum, and Serverless Framework. I have tried a couple of things till I am not able to get the whole setup working using the latest library versions: My handler:
serverless.yml:
My requirements.txt
When I deploy it using serverless, sls depoy and Invoke the function I ma getting the following error:
I have tried with python 3.8 and 3.7. I feel something is missing here. |
Beta Was this translation helpful? Give feedback.
-
@aroraprince Hello. There is another issue that I think is related to the issue you're describing here #126. The ASGI connection scope is built using information provided in the request context when invoked by API Gateway, it appears you are invoking the function without the request context here. |
Beta Was this translation helpful? Give feedback.
-
@jordaneremieff yes, I think it is not able to pass the request context. |
Beta Was this translation helpful? Give feedback.
-
A quick note for anyone who is fresh to deploying FastAPI via AWS API Gateways as a Proxy resource - you must format your response in the following manner: msg = useful_function(accept_arguments)
headers = { # example headers
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
}
return {
'statusCode': 200,
'headers': headers,
'body': json.dumps(msg)
} Otherwise, you can have all sorts of fun dealing with CORS errors having otherwise correctly configured everything else within FastAPI and AWS. |
Beta Was this translation helpful? Give feedback.
-
For anyone else who runs into this issue, just to elaborate on what @TAJD said... Using the https://github.com/chgangaraju/fastapi-mangum-example/blob/master/app/main.py And shown below for your convenience, with comments. #!/usr/bin/env python3
import uvicorn
from fastapi import FastAPI
from mangum import Mangum
# TODO: Add this line
from starlette.middleware.cors import CORSMiddleware
app = FastAPI(title="FastAPI Mangum Example", version='1.0.0')
# TODO: Add these lines
app.add_middleware(
CORSMiddleware,
allow_origins='*',
allow_credentials=False,
allow_methods=["GET", "POST", "OPTIONS"],
allow_headers=["x-apigateway-header", "Content-Type", "X-Amz-Date"],
)
handler = Mangum(app)
@app.get('/', name='Hello World', tags=['Hello'])
def hello_world():
return {"Hello": "Python"}
if __name__ == '__main__':
uvicorn.run(app) |
Beta Was this translation helpful? Give feedback.
-
i'm having same issue and is impossible to find a working example with an updated version
|
Beta Was this translation helpful? Give feedback.
-
Experiencing the same issue :( @federicodv Did you manage to resolve it somehow? |
Beta Was this translation helpful? Give feedback.
-
ERROR:
I tried this, but could not import
AWSLambdaAdapter
Can you please provide a working example of this?
Beta Was this translation helpful? Give feedback.
All reactions