r/FastAPI • u/ForceSpike • May 22 '23
Question Using FastApi with AWS Lambda and API Gateway Stages?
I've deployed my fastapi app to aws lambda using the api gateway. I want to try and use the api gateway stages with the lambda aliases but I'm having an issue where I have to set the root path inside the fastapi code to the name of the stage or else I get all sorts of errors. The docs page gives a 403 and other endpoints give a missing authentication error. So if I have a stage named 'prod' I have to set root_path="/prod/" and if its 'staging' I have to use root_path="/staging/". This kind of ruins the whole point of using staging as I can't be changing the codebase in-between deployments. Has anyone here successfully deployed this way before and found a way to utilize the api gateways stages effectively? Thanks.
2
u/aFqqw4GbkHs May 22 '23
Are you deploying a separate lambda for each stage as well? A few tips from what we're doing:
We're using serverless framework to deploy the lambdas and API gateway to each stage. We set up custom sub domains in Route53 for each stage.
Our handler uses Magnum as the asgi handler: i.e
self.app.add_middleware( CORSMiddleware, allow_origins=[""], allow_credentials=True, allow_methods=[""], allow_headers=["*"], )
self.app.add_exception_handler(Exception, self.default_exception_handler)
3. then, our functions just use decorators with the relative path, e.g