/tutorial-azure-app-service

Deploy Python apps with Azure App Service

Primary LanguagePython

Tutorial for Azure App Service deployment

Deploy a Python Streamlit app with Azure App Service

Azure CLI

Setup

  1. Install Azure CLI
  2. Authenticate with Azure CLI

Azure App Service

For ease of deployment, we will use Azure App Service for Linux which allows developers to deploy web applications with a PaaS experience, similar to Heroku.

Supported web frameworks in Python are Flask and Django. For other non-supported frameworks such as FastAPI, a Docker image is required.

First step is to make sure a ressource group is available. If not, a new one can be created:

az group create \
    --name tutorial-azure-app-service-group \
    --location westeurope

Flask app deployment

As Flask is one of the two Python frameworks natively supported by Azure App Service (the other one being Django), we are going to take advantage of this and deploy a simple Flask app using Git!

Documentation:

  1. Configure a deployment user (might require git config --system --unset credential.helper before-hand):
az webapp deployment user set --user-name <username> --password <password>
  1. Create a Git enabled app
az appservice plan create \
    --name tutorial-azure-app-service-flask-plan \
    --resource-group tutorial-azure-app-service-group \
    --is-linux \
    --sku F1

az webapp create \
    --name tutorial-azure-app-service-flask-app \
    --resource-group tutorial-azure-app-service-group \
    --plan tutorial-azure-app-service-flask-plan \
    --runtime "PYTHON|3.8" \
    --deployment-local-git
  1. Configure the deployment branch:
az webapp config appsettings set \
    --name tutorial-azure-app-service-flask-app \
    --resource-group tutorial-azure-app-service-group \
    --settings DEPLOYMENT_BRANCH='master'
  1. Add the Git remote:
git remote add azure-flask https://tutorial-azure-app-service-flask-app.scm.azurewebsites.net/tutorial-azure-app-service-flask-app.git
  1. Deploy to Azure:
git push azure-flask master
  1. Open the app: https://tutorial-azure-app-service-flask-app.azurewebsites.net

  2. To delete the app:

az webapp delete \
    --name tutorial-azure-app-service-flask-app \
    --resource-group tutorial-azure-app-service-group

Streamlit app deployment

In order to deploy a Streamlit app on Azure App Service, we will need to build and deploy a Docker container.

Documentation: Azure App Service Custom Container

  1. Build and run the Docker image locally:
docker build -t tutorial-azure-app-service-streamlit-app:latest .
docker run --rm -it -p "5000:5000" tutorial-azure-app-service-streamlit-app:latest
  1. Create an Azure Container Registry to push the Docker image:
az acr create \
    --name tutorialazureappserviceregistry \
    --resource-group tutorial-azure-app-service-group \
    --sku Basic \
    --admin-enabled true
  1. Get the credentials for Azure Container Registry:
az acr credential show \
    --name tutorialazureappserviceregistry \
    --resource-group tutorial-azure-app-service-group
  1. Log into Azure Container Registry using Docker:
docker login tutorialazureappserviceregistry.azurecr.io --username tutorialazureappserviceregistry
  1. Tag and push the Docker image to Azure Container Registry:
docker tag tutorial-azure-app-service-streamlit-app tutorialazureappserviceregistry.azurecr.io/tutorial-azure-app-service-streamlit-app:latest
docker push tutorialazureappserviceregistry.azurecr.io/tutorial-azure-app-service-streamlit-app:latest
  1. Verify the Docker image has been pushed:
az acr repository list --name tutorialazureappserviceregistry
  1. Create the Azure App Service (WARNING: the F1 SKU does not seem to be working with Streamlit and Websockets in general, use at least B1, cf. GitHub issue):
az appservice plan create \
    --name tutorial-azure-app-service-streamlit-plan \
    --resource-group tutorial-azure-app-service-group \
    --is-linux \
    --sku B1

az webapp create \
    --name tutorial-azure-app-service-streamlit-app \
    --resource-group tutorial-azure-app-service-group \
    --plan tutorial-azure-app-service-streamlit-plan \
    --deployment-container-image-name tutorialazureappserviceregistry.azurecr.io/tutorial-azure-app-service-streamlit-app:latest

az webapp config appsettings set \
    --name tutorial-azure-app-service-streamlit-app \
    --resource-group tutorial-azure-app-service-group \
    --settings WEBSITES_PORT=5000
  1. Open the app: https://tutorial-azure-app-service-streamlit-app.azurewebsites.net

  2. Turn on container logging:

az webapp log config \
    --name tutorial-azure-app-service-streamlit-app \
    --resource-group tutorial-azure-app-service-group \
    --docker-container-logging filesystem
  1. Display container logging:
az webapp log tail \
    --name tutorial-azure-app-service-streamlit-app \
    --resource-group tutorial-azure-app-service-group
  1. Whenever a new Docker image is pushed to the registry, restart the App Service:
az webapp restart \
    --name tutorial-azure-app-service-streamlit-app \
    --resource-group tutorial-azure-app-service-group