Hello BERGES Armand,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
I understand that you would like to know how you can use FastAPI routers in azure app functions.
The error (The underlying connection has been closed
) suggests there may be a timeout or connection problem. So, you can try to set longer timeouts in your request or debugging this using Azure's logs and try to review the following settings and configurations:
- Check your
function.json
configuration is correct for the Azure Functions environment. If you don't have it already, you need to create afunction.json
file. - If you use external tools like
wget
, you might run into CORS issues, so you have to add CORS middleware to your FastAPI app like the below:
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Adjust as necessary for security
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
- Set your app to always be warm, it might be a cold start issue.
- Your routes are working fine for
/sample
and/hello/{name}
, but you’re having trouble with/api/v1/users
. Double-check that your FastAPI router setup is correct:
In api_router
:
router.include_router(users.router, prefix="/users", tags=["Users"])
- After making these changes locally, redeploy your app to Azure Functions, and enable "Always On" if you're experiencing cold start issues on Azure.
I hope this is helpful! Do not hesitate to let me know if you have any other questions.
Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.