Create Azure Functions using Python

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

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”)
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”

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” ().

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”

Enter this endpoint in browser “http://localhost:7071/api/Demofn?name=%22Hello%20World%22

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

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!!!

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store