The following are steps to follow to initially setup an account to run Azure App Services.
In a browser, log in to portal.azure.com.
Install the Azure command line interface tools:
brew install azure-cli
alternatively, on Ubuntu, follow the instructions on the azure web site.
Then log in to azure on the command line: (use the login info of the lab DuckID account)
az login
which will redirect you to the browser to verify the log in.
Create a resource group
You don't need to do this if it already exists.
az group create --name sanlab_rg_Linux_westus2 --location westus2
Create an App Service plan
You don't need to do this either if it already exists.
az appservice plan create --name sanlab_asp_Linux_westus2 --sku F1 --is-linux --resource-group sanlab_rg_Linux_westus2
The following are steps to deploy an application for the first time. If the app has already been deployed by someone else, you don't need to do this.
Create an app on Azure App Services as:
az webapp up --sku F1 --location "West US 2" --name message-automation --resource-group sanlab_rg_Linux_westus2 --plan sanlab_asp_Linux_westus2
Reference: Quickstart: Create a Python app in Azure App Service on Linux
App is configured as:
az webapp config set --resource-group sanlab_rg_Linux_westus2 --name message-automation --startup-file "gunicorn --bind=0.0.0.0 --timeout 600 --env MESSAGE_AUTOMATION_SETTINGS=config.py \"src.flask_app:create_app()\""
The environment variable MESSAGE_AUTOMATION_SETTINGS
specify where the
application's configuration is. The configuration include the Apptoto API token,
the REDCap API token, and other configuration. Do not check the configuration
into source control. The bit right at the end ("src.flask_app:create_app()"
)
specifies how the gunicorn WSGI server should start and run the Flask app
in the message-automation package.
Reference: Configure a Linux Python app for Azure App Service
az webapp log config --resource-group sanlab_rg_Linux_westus2 --name message-automation --docker-container-logging filesystem
az webapp config set --resource-group sanlab_rg_Linux_westus2 --name message-automation --linux-fx-version "PYTHON|3.8"
Configure Azure App Service to install dependencies (via pip
).
az webapp config appsettings set --resource-group sanlab_rg_Linux_westus2 --name message-automation --settings SCM_DO_BUILD_DURING_DEPLOYMENT=true
This application is deployed using ZIP file deployments so that the configuration file that is not stored in git or github can be added to the ZIP file and uploaded to Azure.
First, change to the git repo directory, then create a zip file from the src/
, tests/
and instance/
directories, and the requirements.txt file.
Then deploy the app with the following command:
az webapp deployment source config-zip --resource-group sanlab_rg_Linux_westus2 --name message-automation --src message_automation.zip
- Make sure the github repo has the most updated scripts
- Pull the most updated repo to any local environment
- Get the current .env and messages.csv file from Azure under the instance folder (These two files are not sync on github).
Log in to Azure portal -> go the the
message-automation
app service -> Development Tools -> SSH And the file is located at
ls instance/
Save the instance directory to the local environment.
4. Double check the .env
and make sure all the API tokens are correct
5. create a new zip file from the src/
, tests/
and instance/
directories, as well as .env and requirements.txt
6. Deploy a test version of the app
az webapp deploy --resource-group sanlab_rg_Linux_westus2 --name message-automation-dev --src-path message_automation.zip --async true
- Check that things work correctly at https://message-automation-dev.azurewebsites.net/
- Redeploy the app
az webapp deploy --resource-group sanlab_rg_Linux_westus2 --name message-automation --src-path message_automation.zip --async true
Event logs and error messages are stored at Development Tools -> Advanced Tools -> Current Docker logs
If you are getting 502 Bad Gateway errors, consider reducing the number of events uploaded at a time in post_events in apptoto.py.