Create Azure Functions using Python

Veera Ganesh
6 min readAug 31, 2020

In this post, we are mainly going to see how to create Azure Functions using Python.

When creating function, Microsoft has provided us to choose languages like C#, JavaScript, Python, Power Shell, Java and so on..

With our convenient, we can write a program in any language.

In general, functions on cloud platform says it as a “server-less” compute.

Does it mean that server is not there?
Answer is NO

There should be a server to execute any piece of the program. Here, server-less means the actual server does not reside at our own premises or own infrastructure. The actual server would reside at cloud which provides by some gigantic providers as per our subscription.

In simple terms, we subscribe to the cloud platform like Azure or GCP or AWS to host our programs or set up an infrastructure with cloud providers.

Cloud providers manage the resources at data center as per our choice when we do the subscription by selecting the region.

Hope, now it is clear on the term “Server-Less” Compute.

Before going to start, we must have some Pre-requisites in order to run our program in the Azure Cloud platform

Pre-requisites:

  1. Azure Subscription — click here
  2. Visual Studio Code (VS Code) — click here to download
  3. Python 3.6 or more — click here to download
  4. Azure CLI (Command Line-Interface) — to access Azure resources using a set of azure command lines. Click here to download.
  5. Azure Core tools — it helps us to run our azure function locally. Refer this link to install Azure Core tools
    Run this command : npm install -g azure-functions-core-tools@2/3

I have already created a resource group in my Azure subscription.

Create Resource Group : To know how to create azure resource group, please visit Microsoft documentation here
Note : We can also create resource group using Azure CLI.

To host function, we need resource group. You can click the above link on how to create resource group. For this demo, I have created resource group in my account.
Let’s see how to create Azure Function using VS Code in step-by-step:

1. In Visual Studio Code > Open Folder > Demo (created this folder for demo purpose)

2. Check if Azure CLI is installed > type this cmd “az — version

3. Check if Azure Core Tools installed > type this cmd “func — version

4. Login to Azure using command line > Type this command “az login” and it will redirect to azure login. Select your account to proceed further.
After successful login, you can able to see all your subscription details in terminal window.

5. Check if python installed using this command > py — version

6. Initialize Function > Type this command “func init” and it will return with option to select worker run time.

7. After selecting worker run time > these files will be created, host.json, local.settings.json and requirements.txt

1. host.json — it has details like versions, logging — application insight
2. local.settings.json — it has Function Worker Runtime and AzurewebJobStorage (by default it has UseDevelopmentStorage=true”)
Note : if we work with azure blob storage like tables, containers from local then we have replace “UseDevelopmentStorage=true” with our storage account name.
3.
requirements.txt — this file will help to install python dependency libraries during publishing stage

8. Create New Function > type this cmd “func new” and it will display the templates to choose.
In this post, we are going to see the “HttpTrigger” and going to select it.

9. Enter Function Name after selecting template “HttpTrigger”

10. After function has created > Folder will be created with Function name as “Demofn” which has provided on previous step.

Inside “Demofn” folder, there will be two main files:
1. __init__py : is main file to execute our program on Azure Function
2. function.json : in this file, we have bindings like authorization, type of template (HttpTrigger), direction, parameter, methods (get, post) — we can remove any one of method as per requirement.

Congrats!!! Azure Function created successfully.

11. Create Virtual Environment — Type this command > “py -m venv .venv”
This command creates folder “.venv” and inside this folder “.venv”, we have sub-folders - “Lib\site-packages” and “Scripts”

1. Lib\site-packages -
have all dependency libraries and
2. Scripts
(will have python executable files which help to run the python code)

12. Activate Virtual Environment : Type this command > “.venv\scripts\activate”
This command activate virtual environment to recognize any pip commands.

13. Install any dependency libraries : like numpy or pandas or tensorflow or spacy etc.,

For demo purpose, I am going to install “numpy” (which is helps to create power multi-dimensional array, or any high-level mathematical functions to operate on arrays).

Type this command > pip install numpy
If you notice here, as mentioned in previous step any dependency library will be added under Lib\Site-Packages

14. Run the Azure Function Locally : Type this command “func start”
Now, function has started and listening to below endpoint.
Demofn: [GET,POST] http://localhost:7071/api/Demofn

Enter this endpoint in browser “http://localhost:7071/api/Demofn?name=%22Hello%20World%22
Note : this function works on both methods “GET/POST”. If you want to use post method, then test in any API test tools like POSTMAN or Fiddler etc.,

Note : We cannot edit the function in azure portal in-line editor since we have chosen Python as run-time worker. So we have to edit from local machine using editor like VS Code.

14. Deployment : Before start deployment, we have to list down all dependency libraries in file “requirements.txt”.
This file helps to install the libraries in Azure portal. I am going to add “numpy” for demo purpose.

No waiting. Let’s deploy or publish it to our Azure Function App. I’have already created Azure Function App in my account.

Type this command to publish > func azure functionapp publish [FunctionAppName] — build remote
Note : Replace [FunctionAppName] with your function app

That’s all. I think we have covered everything.

Hope, this post would help you to create Azure Function using Python.

If you are interested to create Cloud Function in Google Cloud Platform. Please visit my another article here

Happy Learning!!!

--

--

Veera Ganesh

Expertise in Microsoft Azure, SharePoint, Power Platform, .Net, Azure AI, Google Cloud Platform, AI - Chatbot, and Javascript Framework