Skip to content

Azure-Samples/msdocs-fastapi-postgresql-sample-app

Repository files navigation

page_type languages products urlFragment name description
sample
azdeveloper
python
bicep
html
css
scss
azure
azure-app-service
azure-postgresql
azure-virtual-network
msdocs-fastapi-postgresql-sample-app
Deploy FastAPI application with PostgreSQL on Azure App Service (Python)
This project deploys a restaurant review web application using FastAPI with Python and Azure Database for PostgreSQL - Flexible Server. It's set up for easy deployment with the Azure Developer CLI.

Deploy FastAPI application with PostgreSQL via Azure App Service

This project deploys a web application for a restaurnant review site using FastAPI. The application can be deployed to Azure with Azure App Service using the Azure Developer CLI.

Run the sample

This project has a dev container configuration, which makes it easier to develop apps locally, deploy them to Azure, and monitor them. The easiest way to run this sample application is inside a GitHub codespace. Follow these steps:

  1. Fork this repository to your account. For instructions, see Fork a repo.

  2. From the repository root of your fork, select Code > Codespaces > +.

  3. In the codespace terminal, run the following commands:

    # Create .env with environment variables
    cp .env.sample.devcontainer .env
    
    # Install requirements
    python3 -m pip install -r src/requirements.txt
    
    # Install the app as an editable package
    python3 -m pip install -e src
    
    # Run database migrations
    python3 src/fastapi_app/seed_data.py
    
    # Start the development server
    python3 -m uvicorn fastapi_app:app --reload --port=8000
  4. When you see the message Your application running on port 8000 is available., click Open in Browser.

Running locally

If you're running the app inside VS Code or GitHub Codespaces, you can use the "Run and Debug" button to start the app.

python3 -m uvicorn fastapi_app:app --reload --port=8000

Deployment

This repo is set up for deployment on Azure via Azure App Service.

Steps for deployment:

  1. Sign up for a free Azure account and create an Azure Subscription.

  2. Install the Azure Developer CLI. (If you open this repository in Codespaces or with the VS Code Dev Containers extension, that part will be done for you.)

  3. Login to Azure:

    azd auth login
  4. Provision and deploy all the resources:

    azd up

    It will prompt you to provide an azd environment name (like "myapp"), select a subscription from your Azure account, and select a location (like "eastus"). Then it will provision the resources in your account and deploy the latest code. If you get an error with deployment, changing the location can help, as there may be availability constraints for some of the resources.

  5. When azd has finished deploying, you'll see an endpoint URI in the command output. Visit that URI, and you should see the front page of the app! 🎉

  6. When you've made any changes to the app code, you can just run:

    azd deploy

Getting help

If you're working with this project and running into issues, please post in Issues.

About

Deploy a Python FastAPI web app with PostgreSQL in Azure

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published