I em trying to run a FastAPI on Azure Web App. But getting following error:
azure 504.0 GatewayTimeout
When I look in the logs I get following error:
Any suggestions what can cause this?
name: Build and deploy Python app to Azure Web App - segato-fast-api
on:
push:
branches:
- dev
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python version
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Create and start virtual environment
run: |
python -m venv venv
source venv/bin/activate
- name: Install dependencies
run: pip install -r requirements.txt
# Optional: Add step to run tests here (PyTest, Django test suites, etc.)
- name: Zip artifact for deployment
run: zip release.zip ./* -r
- name: Upload artifact for deployment jobs
uses: actions/upload-artifact@v4
with:
name: python-app
path: |
release.zip
!venv/
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: 'Production'
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
permissions:
id-token: write #This is required for requesting the JWT
steps:
- name: Download artifact from build job
uses: actions/download-artifact@v4
with:
name: python-app
- name: Unzip artifact for deployment
run: unzip release.zip
- name: Login to Azure
uses: azure/login@v2
with:
client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_B7A634A93F634F01A9CEAF7F209FCFD4 }}
tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_085412552C7445619A1BD2878A692F63 }}
subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_B0970C502F86483D9551067BC403A7CA }}
- name: 'Deploy to Azure Web App'
uses: azure/webapps-deploy@v3
id: deploy-to-webapp
with:
app-name: 'segato-fast-api'
slot-name: 'Production'
Add or update the Azure App Service build and deployment workflow config · hafnia-dna/dna-port-api@561d351
from fastapi import FastAPI,Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi_azure_auth import SingleTenantAzureAuthorizationCodeBearer
import uvicorn
from fastapi import FastAPI, Security
from settings import Settings
from contextlib import asynccontextmanager
from typing import AsyncGenerator
from enums import settings
from routers.router1 import router1_router
from routers.docs import tags_metadata
from utils.azure.azure_auth import AzureAuth
from utils.azure.azure_logging import azure_logger
azure_auth = AzureAuth()
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
await azure_auth.load_openid_config()
yield
app = FastAPI(
openapi_tags=tags_metadata,
swagger_ui_oauth2_redirect_url='/oauth2-redirect',
swagger_ui_init_oauth={
'usePkceWithAuthorizationCodeGrant': True,
'clientId': settings.OPENAPI_CLIENT_ID,
'scopes': settings.SCOPE_NAME,
},
)
app.include_router(router1_router)
if settings.BACKEND_CORS_ORIGINS:
app.add_middleware(
CORSMiddleware,
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'],
)
if __name__ == '__main__':
uvicorn.run('main:app', reload=True)
Configuration:
these 504 error mostly means the container does not startup. likely code crashed on startup. my suggestion is to retrieve more error logs than what you can see in the log stream
. what i usually do is to connect to the app service via VS code
's azure app service
extension, then you will see a detailed container log file here. it usually tells you the initial crash exception. You should be able to see the same logs using kudo console.