FastAPI Application Deployment on Azure App Service: 503 Service Unavailable Error

111 Views Asked by At

Issue Summary: I am experiencing a 503 Service Unavailable error when deploying a FastAPI application on Azure App Service (Linux). The application runs fine locally but encounters issues when deployed to Azure.

Environment:

FastAPI version: 0.108.0 Gunicorn version: 21.2.0 Uvicorn version: 0.25.0 Deployment: Azure App Service on Linux

Application Structure: The application is a basic FastAPI setup:

python

# app.py
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

@app.get("/")
async def read_root():
    return {"Hello": "World"}

@app.get("/items/{item_id}")
async def read_item(item_id: int):
    return {"item_id": item_id}

Requirements (requirements.txt):

fastapi==0.108.0
gunicorn==21.2.0
uvicorn==0.25.0

Deployment Details: I deployed this application to Azure App Service. I didn't use a Procfile, but I set the startup command in the App Service configuration under General Settings → Startup:

gunicorn -w 4 -k uvicorn.workers.UvicornWorker -b :80 app:app

Issue Encountered: When accessing the application URL (e.g., https://test-api-qa.azurewebsites.net/items/1), I receive a 503 Service Unavailable error.

2024-01-05T15:21:11.509Z ERROR - Container test-api-qa_1_ebb38e7f didn't respond to HTTP pings on port: 8000, failing site start. See container logs for debugging.
2024-01-05T15:21:11.514Z INFO  - Stopping site test-api-qa because it failed during startup.
2024-01-05T15:27:21.578Z INFO  - 3.10_20230810.1.tuxprod Pulling from appsvc/python
2024-01-05T15:27:21.589Z INFO  -  Digest: sha256:6e7907b272357dfda9a8c141b01fc30851ffc4448c6c41b81d6d6d63d2de0472
2024-01-05T15:27:21.590Z INFO  -  Status: Image is up to date for 10.1.0.4:13209/appsvc/python:3.10_20230810.1.tuxprod
2024-01-05T15:27:21.603Z INFO  - Pull Image successful, Time taken: 0 Seconds
2024-01-05T15:27:21.634Z INFO  - Starting container for site
2024-01-05T15:27:21.635Z INFO  - docker run -d --expose=8000 --name test-api-qa_1_966ce6a6 -e WEBSITE_USE_DIAGNOSTIC_SERVER=false -e WEBSITE_SITE_NAME=test-api-qa -e WEBSITE_AUTH_ENABLED=False -e PORT=8000 -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=test-api-qa.azurewebsites.net -e WEBSITE_INSTANCE_ID=b11d819b8697339cf9054171dc33b13ea31a05e43536bee7f778d0e63d5fd0c5 -e HTTP_LOGGING_ENABLED=1 appsvc/python:3.10_20230810.1.tuxprod gunicorn -w 4 -k uvicorn.workers.UvicornWorker -b :80 app:app
2024-01-05T15:27:22.048Z INFO  - Initiating warmup request to container test-api-qa_1_966ce6a6_msiProxy for site test-api-qa
2024-01-05T15:27:22.057Z INFO  - Container test-api-qa_1_966ce6a6_msiProxy for site test-api-qa initialized successfully and is ready to serve requests.
2024-01-05T15:27:22.058Z INFO  - Initiating warmup request to container test-api-qa_1_966ce6a6 for site test-api-qa
2024-01-05T15:27:52.435Z INFO  - Waiting for response to warmup request for container test-api-qa_1_966ce6a6. Elapsed time = 30.3869829 sec
2024-01-05T15:28:07.503Z INFO  - Waiting for response to warmup request for container test-api-qa_1_966ce6a6. Elapsed time = 45.4549341 sec
2024-01-05T15:28:22.783Z INFO  - Waiting for response to warmup request for container test-api-qa_1_966ce6a6. Elapsed time = 60.7352662 sec
2024-01-05T15:28:37.855Z INFO  - Waiting for response to warmup request for container test-api-qa_1_966ce6a6. Elapsed time = 75.8073171 sec
2024-01-05T15:28:52.924Z INFO  - Waiting for response to warmup request for container test-api-qa_1_966ce6a6. Elapsed time = 90.8760207 sec
2024-01-05T15:29:07.990Z INFO  - Waiting for response to warmup request for container test-api-qa_1_966ce6a6. Elapsed time = 105.9423549 sec
2024-01-05T15:29:23.059Z INFO  - Waiting for response to warmup request for container test-api-qa_1_966ce6a6. Elapsed time = 121.0115291 sec
2024-01-05T15:29:38.128Z INFO  - Waiting for response to warmup request for container test-api-qa_1_966ce6a6. Elapsed time = 136.0803053 sec
2024-01-05T15:29:53.194Z INFO  - Waiting for response to warmup request for container test-api-qa_1_966ce6a6. Elapsed time = 151.1457954 sec
2024-01-05T15:30:08.258Z INFO  - Waiting for response to warmup request for container test-api-qa_1_966ce6a6. Elapsed time = 166.2100867 sec
2024-01-05T15:30:23.323Z INFO  - Waiting for response to warmup request for container test-api-qa_1_966ce6a6. Elapsed time = 181.2752611 sec
2024-01-05T15:30:38.387Z INFO  - Waiting for response to warmup request for container test-api-qa_1_966ce6a6. Elapsed time = 196.3392949 sec
2024-01-05T15:30:53.452Z INFO  - Waiting for response to warmup request for container test-api-qa_1_966ce6a6. Elapsed time = 211.40458 sec
2024-01-05T15:31:08.525Z INFO  - Waiting for response to warmup request for container test-api-qa_1_966ce6a6. Elapsed time = 226.4769965 sec
2024-01-05T15:31:12.550Z ERROR - Container test-api-qa_1_966ce6a6 for site test-api-qa did not start within expected time limit. Elapsed time = 230.5018293 sec
2024-01-05T15:31:12.554Z ERROR - Container test-api-qa_1_966ce6a6 didn't respond to HTTP pings on port: 8000, failing site start. See container logs for debugging.
2024-01-05T15:31:12.557Z INFO  - Stopping site test-api-qa because it failed during startup.

Attempts to Resolve:

Checked that the application works locally. Verified that the Procfile is not used and the startup command is set correctly in Azure. Considered potential port configuration issues.

Questions:

  1. Why might Azure App Service be failing to serve requests to my FastAPI application?
  2. Is there a mismatch between the container's exposed port and the port that Azure expects the app to be served on?
  3. Are there additional configurations or logs that I should look into for troubleshooting this issue?

Any insights or suggestions from the community would be greatly appreciated!

1

There are 1 best solutions below

0
On

This issue was resolved by removing the port from the command.

gunicorn -k uvicorn.workers.UvicornWorker app:app

And also realized workers threads were not important to pass. The above command worked well