Login to your Azure Account
- Create a new Azure account if you do not already have one
- Login at portal.azurce.com
Reference Material
Please follow the Hints
Create a new Azure Account (if you don’t have one or desired):
- If you are associated with a Microsoft Partner get $100 a month here: https://signup.azure.com/signup?offer=MS-AZR-0025P
- If you have an MSDN Pro subscription get $50 a month here: https://azure.microsoft.com/en-us/offers/ms-azr-0059p/
- If you have an MSDN Platform subscription get $100 a month here: https://azure.microsoft.com/en-us/offers/ms-azr-0062p/
- If you have an MSDN Enterprise subscription get $150 a month here:
- If you have none of these get a one time $200 credit here: https://azure.microsoft.com/en-us/offers/ms-azr-0044p/
Login to Azure:
- Go to https://portal.azure.com/
- Log in with the same credentials you used to create your Azure account, these could have been AAD Work credentials or Microsoft Account credentials
Create the Azure Resources and Configure
- Create a new Resource Group named: workshop-rg
Reference Material
https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-portal
Follow these steps:
- Select Resource Group on the Menu
- Click Add
- Resource Group Name = workshop-rg
- Resource Group Location = Central US
- Click Create
- Wait until creation is complete, click refresh from the resource group view, you should see your resource group appear
- Create a Web App in this resource group named: yourname-app
- OS = Windows
- Publish = Code
- App Insights = Off
- Attach a new App Service plan = New ASP named: yourname-asp with pricing tier Standarad: 1 Small
Reference Material
https://azure.microsoft.com/en-us/get-started/web-app/
Follow these steps:
- Select Resource Group on the Menu
- Click workshop-rg
- Click Add
- Search for Web App and select it
- Click Create
- App Name = yourname-app (each must be universally a unique name, pick one that makes sense to you)
- Resource Group = Use Existing, workshop-rg
- OS = Windows
- Publish = Code
- App Service Plan/Location = Create New
- App Service Plan = yourname-asp
- Location = Central US
- Pricing Tier = S1 Standard
- Application Insights = Off
- Click Create
- Wait until creation is complete, click refresh from the workshop-rg view, you should see 2 items in your resource group
- Configure your web app with:
- Python = 3.4
- Platform = 64-bit
- Always On = On
- Application Settings
- KERAS_BACKEND = cntk
- WSGI_LOG = d:\home\logfiles\log.txt
- Install the Python 3.5.4 x64 extension
Reference Material
https://docs.microsoft.com/en-us/azure/app-service/web-sites-configure
https://azure.microsoft.com/en-us/blog/azure-web-sites-extensions/
Follow these steps:
- Select Resource Group on the Menu
- Click workshop-rg
- Select yourname-app
- Select Application Settings on the secondary menu
- Set Python = 3.4
- Set Platform = 64-bit
- Set Always On = On
- Under Application Settings create these app settings:
- KERAS_BACKEND = cntk
- WSGI_LOG = d:\home\logfiles\log.txt
- Click Save
- Wait until saving is complete
- Select Extensions on the secondary menu
- Click Add
- Click Choose Extension and pick Python 3.5.4 x64
- Accept the Legal terms
- Click OK
- Wait until Extension install completes
Configure deployment
- Use Deployment Center to configure Local GIT deployment
- Set the Deployment Password
Reference Material
https://docs.microsoft.com/en-us/azure/app-service/app-service-deploy-local-git
Follow these steps:
- Open yourname-app
- Click Deployment Center from the secondary menu
- Select Local Git
- Click Continue
- Select App Service Kudu
- Click Continue
- Click Finish
- Click Deployment Credentials
- Click User Credentials
- Enter a password of your choice twice and click save credentials
- If you have a Git tool you know how to use to clone, commit, push, and pull you can skip this step
- Download and install SourceTree
Reference Material
https://confluence.atlassian.com/get-started-with-sourcetree/install-sourcetree-847359094.html
- Go to https://id.atlassian.com/signup
- Create and verify your login (if you don’t already have one)
- Go to https://www.sourcetreeapp.com/
- Click download
- Run the downloaded file
- Click through the installer, logging into to your account, only install Git not Mecurial, skip adding remotes
- Clone the repo using the information found in the Azure Web App Deployment Center to a local folder named yourapp-deploy
Reference Material
https://confluence.atlassian.com/bitbucket/clone-a-repository-223217891.html
Find the info used to clone the Git Repo from azure:
- Open yourname-app
- Click Deployment Center from the secondary menu
- Make a note of the Git Clone Uri from this screen
- Use the password you setup in a previous step
Use Sourcetree to clone the repo:
- Create a folder on your hard drive named yourapp-deploy
- Open Sourcetree
- Click the + button near the top of the screen
- Click the Clone button
- Source Path = enter the Uri from Azure that was labeled Git Clone Uri
- Enter your password in the login that pops up
- Destination Path = click browse and select the folder created in step 1
- Click Clone
Perform deployment
- Open the folder yourapp-deploy
- Delete everything besides the .git folder
Reference Material
None
- Copy the main.py and out folder to yourapp-deploy
Reference Material
None
- Create a file named reqs.txt (NOT the normal requirements.txt)
- Define the package requirements within this file
Reference Material
https://devcenter.heroku.com/articles/python-pip
https://pip.pypa.io/en/stable/reference/pip_show/
Create reqs.txt:
- Create a new file in yourapp-deploy named reqs.txt
- Add the requirements in the form of pipmodule==version number, one per line, use pip list and pip show to find them
Use the following as your contents of reqs.txt:
numpy==1.11.3 | |
scikit-learn==0.18.1 | |
click==6.7 | |
Flask==0.12.2 | |
itsdangerous==0.24 | |
Jinja2==2.9.6 | |
MarkupSafe==1.0 | |
Werkzeug==0.12.2 | |
cntk-gpu==2.6 | |
scipy==1.0.0 | |
keras==2.0.6 | |
h5py==2.7.1 |
- Copy ptvs_virtualenv_proxy.py from the Python Tools for Visual Studio in yourapp-deploy
- Create a web.config with the contents of hint 2
Reference Material
https://github.com/Microsoft/PTVS
Download this file and place it in yourapp-deploy folder: https://github.com/Microsoft/PTVS/blob/master/Python/Product/BuildTasks/ptvs_virtualenv_proxy.py
Create the web.config file:
- Create a new web.config file in yourapp-deploy folder.
- Place these contents in the file and save:
<?xml version="1.0"?> | |
<configuration> | |
<appSettings> | |
<add key="WSGI_ALT_VIRTUALENV_HANDLER" value="main.app" /> | |
<add key="WSGI_ALT_VIRTUALENV_ACTIVATE_THIS" | |
value="d:\home\python354x64\python.exe" /> | |
<add key="WSGI_HANDLER" | |
value="ptvs_virtualenv_proxy.get_venv_handler()" /> | |
<add key="PYTHONPATH" value="D:\home\site\wwwroot" /> | |
</appSettings> | |
<system.web> | |
<compilation debug="true" targetFramework="4.0" /> | |
<customErrors mode="Off" /> | |
</system.web> | |
<system.webServer> | |
<httpErrors errorMode="Detailed"></httpErrors> | |
<modules runAllManagedModulesForAllRequests="true" /> | |
<handlers> | |
<remove name="Python27_via_FastCGI" /> | |
<remove name="Python34_via_FastCGI" /> | |
<add name="Python FastCGI" | |
path="handler.fcgi" | |
verb="*" | |
modules="FastCgiModule" | |
scriptProcessor="d:\home\python354x64\python.exe|d:\home\python354x64\wfastcgi.py" | |
resourceType="Unspecified" | |
requireAccess="Script" /> | |
</handlers> | |
<rewrite> | |
<rules> | |
<rule name="Static Files" stopProcessing="true"> | |
<conditions> | |
<add input="true" pattern="false" /> | |
</conditions> | |
</rule> | |
<rule name="Configure Python" stopProcessing="true"> | |
<match url="(.*)" ignoreCase="false" /> | |
<conditions> | |
<add input="{REQUEST_URI}" pattern="^/static/.*" ignoreCase="true" negate="true" /> | |
</conditions> | |
<action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="true" /> | |
</rule> | |
</rules> | |
</rewrite> | |
</system.webServer> | |
</configuration> |
- Open an Anaconda Prompt in your CNTK environment
- Change to the yourapp-deploy folder
- Create a wheelhouse based off of the reqs.txt file
Reference Material
https://pypi.org/project/Wheelhouse/
https://docs.python-guide.org/dev/virtualenvs/
Create the wheelhouse:
- Open an Anaconda Prompt
- Activate cntk-py35
- Use the “cd” command to change to the folder yourapp-deploy
- python.exe -m pip wheel -r reqs.txt -w wheelhouse
- Wait while the wheelhouse is built, it will take a long time
- Create a file named .deployment in yourapp-deploy
- Add these contents to the file:
[config] command = deploy.cmd
- Create a file named deploy.cmd
- Add these contents to the file:
d:\home\python354x64\python.exe -m pip install --upgrade --force-reinstall --ignore-install -r reqs.txt
Reference Material
None
- Commit the changes in yourapp-deploy
- Push changes and wait, watching the output from the push
Reference Material
Commit the Changes:
- Open Source Tree
- Select the tab for yourapp-deploy
- Click the commit toolbar button
- Click the stage all button
- Enter a comment
- Click Commit
Commit the Changes:
- Click the Push button the toolbar
- On the next dialog check the checkbox to watch the commit
- Wait a long time, you are waiting for the push, the installation of the python dependencies, this will take a while
Test with CURL
- Open an Anaconda prompt and switch to your Sales-Win-Loss folder
- curl -X POST -d @request.json “http://yourapp-app.azurewebsites.net/predict” -H “Content-Type: application/json”
- Verify you get a json document back with a prediction
Reference Material
None
Lab Complete!
Extra Credit – Python in Azure
Article on deploying Python Modules to Azure App Services:
Profiling Python in Azure:
Profile Python Applications in Azure App Services
Logging in Python Apps in Azure:
Troubleshoot- logging python Application errors on Azure Web/API Apps