This tutorial will show how to create a Python straight from your browser, with free Azure resources only. You will learn how to deploy a Cosmos DB Account, and a Web App from Git using the Azure Cloud Shell without installing any software in your local computer
Based on SARATH LAL’s https://codewithsarath.com/a-simple-to-do-python-flask-application-with-mongodb/ and Microsoft’s https://docs.microsoft.com/en-us/azure/cosmos-db/create-mongodb-flask/
- A computer with a modern web browser (Edge, Chrome or Firefox)
- Free Azure Account https://azure.microsoft.com/en-us/free/
- Free Azure Cosmos DB’s API for MongoDB
- Azure Cloud Shell
First of all you need to create a free Azure account using this link https://azure.microsoft.com/en-us/free/ it provide some free credits on the first month, free services for 12 months and other free services forever. When you get your account you will use the URL https://portal.azure.com/ to access your Azure GUI.
Create an Azure Cosmos DB’s API for MongoDB account
Step 1: On portal.azure.com top search bar type “cosmos” and click on “Azure Cosmos DB”
Step 2: Click on +Add
Step 3: Fill the form:
- Subscription: Select your free subscription (Free Trial)
- Resource Groups: Create a new resource group, Remember this you use it on the next step and it will make easy to delete this deployment on the future.
- Account Name: Choose a name for your account.
- API: Select Azure Cosmos DB for MongoDB API
- Notebooks: Select Off
- Apply Free Tier Discount: Select Apply, it will apply your free tier discount to use Cosmos DB for free.
- Location: Select the closest locations for your users, it will provide a responsive app.
- Account Type: Non-Production. It is only a tag, you can choose whatever you want.
- Version: Select 3.6
- Geo-Redundancy: Select Disable. In this example, we will use only one geographic location, but if you are deploying a highly available service you should select yes.
- Multi-region Writes: Select Disable. When you deploy an App with a global footprint, it will accelerate your app world widely.
Setp 4: On the screenshot above click on review and create, then click on create (illustrated below).
Step 5: Access your Azure Cloud Shell, clicking on the terminal icon on the Azure’s Portal top bar
Step 6: If it is your first time create the storage account to work with the Cloud Shell. This account use Azure Files, a service that allows you to access this files on your computer as a network share. Remember, at the time I’m writing this article Azure gives away free 5Gb storage on the first 12 months.
Step 7: Now we will clone the Github repository on our Azure Cloud Shell. We will use the git clone command with our repository URL
git clone https://github.com/azuretar/python-cosmosdb-azure-webapp-tutorial.git
Step 8: Now we have the source code, let’s change the directory to the cloned git
Step 9: To test the App we need to Install the Python requirements, we will do that using pip, with the following command
pip install -r requirements.txt
Step 10: Now we need to edit the code to insert the Cosmos DB details. We will use the code editor included on the Azure Cloud Shell, to open the editor use the following command:
Step 11: We need to edit the app.py, open it clicking on app.py and you will view the file opened on editor.
Step 12: Now we need to get the database details to connect our app, so open a new tab on portal.microsoft.com in your browser and Access the Cosmos DB account you have created, click in Settings /Connection String and copy the PRIMARY CONNECTION STRING
Step 13: Now you can go back to the code editor and paste the Primary Connection String between the signalled quotes and save and close the editor.
Python Tip: This “r” before the Primary Connection String makes the Python accept the “&” character included on the string.
Step 14: Let’s test the app. Run the code with the following command:
Step 15: To be able to see the website preview, Click on Web Preview icon (highlighted), click on Configure, type the 5000, and click open and browse, check if the site opens
Step 16: Now you have seen the app working. go back to the Cloud Shell and stop the code pressing CTRL+C
Step 17: Now let’s publish this App on Azure Web App via AZ CLI. We could do that using the Azure Web interface, but now let’s try an AZ CLI Command
az webapp up --sku F1 --name <name of your application> --resource-group <use the same resource group you>
The AZ CLI stands for Azure Command Line Interface, which can deploy anything to Azure and helps a lot with automation. The AZ Cli comes pre-configured to the Azure Cloud Shell. Bellow the command explanation. You need write the command with your own application name and resource group.
- az is the main command
- webapp is the command to manage Web Apps
- up is the command to deploy your Web App
- –sku let you select the WebApp size, in this case we are using the F1 that is the Free Tier
- –name is where you specify your app name. Your Web App URL you be https://<name>.azurewebsites.net/
- –resource-group is where you specify your resource group. Use the same as your Cosmos DB Account
Step 18: Now let’s check what our az command has created. On portal.microsoft.com search for App Services, click on the Web App you have created, then click on the App URL and check it working!
Step 19 (Optional): Everything you have created in this tutorial is free, you can keep online, but if you want to delete. Type Resource Groups on the portal search bar
Fird the resource group you have deployed your Web App and Cosmos DB and click on it. You will see all your resources inside, then click on “Delete resource group”
Type the recource group name and click on delete.
After a few minutes all resources will be deleted. You can Create another free Cosmos DB and WebApp again.