pythonazureazure-web-app-servicefastapi

Deploying FastAPI in Azure


I am trying to deploy my application which is built using Python and FastAPI for backend and the HTML for the frontend.

Using the student login i created an app service and uploaded my code using github.

My project directory is like

Frontend/
|- file.html
|- x.css
FastAPI/
|- main.py
|- other.py
|- requirements.txt

in my .yml files, I changed the directory of the requirements.txt and package in deploy to ./fastAPI. But the website url shows the following message.

Hey, Python developers!

Your app service is up and running. Time to take the next step and deploy your code.

And it displays the error message in github workflow

Deployment Failed, Error: Failed to deploy web package to App Service. Conflict (CODE: 409)

I tried to use startup-command in the yml file to start the FastAPI, it didn't work and gave the following error message

Deployment Failed, Error: startup-command is not a valid input for Windows web app or with publish-profile auth scheme.

I changed the configuration on the azure portal to add WEBSITES_CONTAINER_STARTUP_COMMAND = uvicorn main:app --host 0.0.0.0 --port 80 in the application settings, didn't work.


Solution

  • I have deployed a simple Fast API project with HTML and CSS as frontend:

    My project structure:

    - FastAPI
        - templates
            - index.html
        -static
            - style.css
        - main.py
        - requirements.txt
    

    enter image description here

    enter image description here

    My Workflow to deploy FastAPI:

    name: Build and deploy Python app to Azure Web App - <web_app_Name>
    
    on:
      push:
        branches:
          - main
      workflow_dispatch:
    
    jobs:
      build:
        runs-on: ubuntu-latest
    
        steps:
          - uses: actions/checkout@v2
    
          - name: Set up Python version
            uses: actions/setup-python@v1
            with:
              python-version: '3.10'
    
          - 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: Upload artifact for deployment jobs
            uses: actions/upload-artifact@v2
            with:
              name: python-app
              path: |
                . 
                !venv/
      deploy:
        runs-on: ubuntu-latest
        needs: build
        environment:
          name: 'Production'
          url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    
        steps:
          - name: Download artifact from build job
            uses: actions/download-artifact@v2
            with:
              name: python-app
              path: .
              
          - name: 'Deploy to Azure Web App'
            uses: azure/webapps-deploy@v2
            id: deploy-to-webapp
            with:
              app-name: '<web_app_Name>'
              slot-name: 'Production'
              publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_XXXXXXXXXXX }}
    Ad
    

    Deployed Successfully:

    enter image description here

    You have to run the Startup command for FastAPI in App Service=>Settings=>Configuration: gunicorn --bind=0.0.0.0 main:app

    enter image description here

    References:

    Refer my SO thread on the similar issue.