How to create Functions in Azure using Python
Tags: python, azure, vazuredevstories, computerscience
Published: 2021-02-10 08:56:09

Recently, I have become a Microsoft learn student ambassador. As I was upgraded to Alpha MLSA, I was given credits to use Microsoft Azure for free along with other benefits. While I was exploring I happened to see Azure Functions and I started learning that. Since my favorite programming language is python, I thought of creating a Azure functions using python. It was pretty exciting. Through this blog post, I'll explain how to create a function using python in Microsoft Azure with the help of Visual Studio Code. 

## Step-1: Configuration of the Environment 

Before getting started, we have to make sure of certain requirements. They are: 
* You need to have a active Microsoft Azure Subscription, If you are a student you can get the Student benefits [from here](https://azure.microsoft.com/en-in/free/students/). If you are not a student you can create an account with a proper subscription. 
* You need to have [azure functions core tools with version 3.x](https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=windows%2Ccsharp%2Cbash#install-the-azure-functions-core-tools).
* You need to install Visual Studio Code and the required extensions (Python extension and Azure Functions Extension).
![1](https://dev-to-uploads.s3.amazonaws.com/i/ftkzgvtdt6whft9uw5wo.PNG)
![2](https://dev-to-uploads.s3.amazonaws.com/i/w4rmeq7pmcy2ffcgne89.PNG)
 

## Sign in to Azure 

1. To sign-in to Azure, you need to choose the Azure icon in the activity bar and in the "Azure:Fuctions" area choose "Sign in to Azure...".
2. When prompted in the browser, you can sign in using your Microsoft Azure account credentials. 
3. After successful sign in you can close the browser and go back to VS code.

## Create the local project and Run the function locally

1. To create the local project, choose the Azure icon in the Activity Bar, then choose "Create New project..." in the "Azure:Fuctions" area, then choose the location for the workspace and select. 
![Screenshot (7)_LI](https://dev-to-uploads.s3.amazonaws.com/i/3lgtob0cc6cngz7xch9e.jpg)

2. After selecting the location, It will prompt for some information. Provide this information at the prompts,

* language for the project: Python 
* Select python alias to create virtual environment: Select the location for python interpreter.
* Template: HTTP Trigger
* Function name: (Choose any unique name of your choice).
* Authorization: Anonymous
* Choose how you would prefer to open this project from the given options.

With the information that you have given above, VScode creates an azure function project with HTTP trigger. 

To run this function,

1. Press F5 key or Choose the Run> Start Debugging to start the function app project, Output is displayed in the Terminal Panel. 
![Screenshot (8)](https://dev-to-uploads.s3.amazonaws.com/i/m2mhlvzbdki81h7ottu1.png)

Here you can see the URL endpoint that HTTP triggered function running locally.

2. Go to "Azure:Fuctions" through Azure icon from the workspace and choose Local Prject>Functions>(your Function) right click on it and select "Execute Function Now..." 
3. Then, Enter request Body will be prompted and you'll see {"name": "azure"}, press Enter to send the request to your function. This will take you to..
![Screenshot (5)](https://dev-to-uploads.s3.amazonaws.com/i/ppbxitz43aktan87d9nh.png)

In the URL type `?name=(any name)` and Press Enter, then
![Screenshot (4)](https://dev-to-uploads.s3.amazonaws.com/i/4xtswgp03y8y7mea5y1b.png)

You can see that the HTTP triggered Successfully. 

4. Go back to the visual studio code and you will see "Function executed successfully" in the terminal panel. Press Ctrl+C to stop and disconnect the debugger. 

Now you have verified that the function runs perfectly on your local computer. Now, lets publish the project to azure using Visual Studio Code. 

## Publish the Project 

1. Choose the Azure Icon in the Activity Bar and select the "Deploy to function app..." button in the "Azure:Functions" area. 
2. Give this information at the prompts,

* Select the folder in which you function exist.
* Select your azure Subscription 
* Choose "Create new Function app" from the prompts. 
* Enter the name for your project based on requirements.
* Select the runtime version of python.
* Select the location for new resources.

The extension will show the status of individual resources as they are being created,
![Screenshot (2)](https://dev-to-uploads.s3.amazonaws.com/i/5wsdvxl66scadalpkkq6.png)

After completion,
![Screenshot (6)](https://dev-to-uploads.s3.amazonaws.com/i/ictix2mswwsw0utjhktg.png)

This is how you can create an Azure Function using python with the help of visual studio code. You can also create the azure functions with command line using Azure CLI and Azure powershell.