Previously, we discussed how we can create an Azure function in Java using IntelliJ IDEA IDE. In this post, we will learn how to create an Azure function in Python using VS Code (Visual Studio Code IDE). We know that Visual Studio Code is an open-source IDE that can be used to build, run and debug the code in an interactive mode.
Azure functions are used to automate and orchestrate complex problems in an event-driven model. It supports multiple languages for development like Java, C#, Python, F#, Powershell, JavaScript, and TypeScript. Here, we will use Python to create a sample Azure Function.
Set up Visual Studio Code for Azure Function Development
To set up a local development environment to create, test, and deploy an Azure Function using Python and VS Code, we need to follow these steps:
1) Download and Install Visual Studio Code IDE and Python
To download the visual studio code, click here. Once the installer is downloaded, follow the on-screen instructions to install it on your local machine.
Next, open the VS code editor, and the below screen will appear.
We also need to download and install the latest version of the Python if we do not have installed Python on our local machine. The latest Python installer can be downloaded and installed from here.
2) Install Azure Toolkit for VSCode
Now, we need to find and install the Azure Functions extensions to the VS Code. It enables Azure Function development in the VS Code IDE. To do so, go to Extensions -> Find Azure Functions extension -> Click on Install
Additionally, we can install the Azure Tools extensions that can help to work with Azure resources.
It may ask to restart the IDE.
3) Create an Azure Function Project in Python
The above-installed extension enables Azure Function development environment in Visual Studio Code. At the left bottom of the IDE, locate the Azure option and click on that button.
Go to Workspace -> click on the + button -> Click on Create Function.
On the next screen, click on the Create a new project button.
Next, provide a location for the new project and click on the Select button.
On the next screen, choose the programming language that we want to use for the Azure function development. Here, we have selected Python.
Next, we need to configure the Python Interpreter installed on the local machine.
Now, we need to select a trigger for the Azure function. In this demo, we are using HTTP Trigger.
Put a name for the Azure function as below. We have used HTTPTriggerTest as the function name here.
Next, configure an authorization level for the function. In this demo, we have used Function as the authorization level.
Finally, select open in the current window option to open this project.
The VS Code may ask to explicitly provide the consent of trust. Click on the Trust this folder option.
Finally, we have an Azure Function App created in Visual Studio Code using Python language.
4) Run and Verify the Azure Function
Now that we have already created an Azure Function, we can run and test this Azure function directly from Visual Studio Code IDE. To run the function, click Run -> Start Debugging.
It will start the Azure function API that can listen to a specific port.
To verify this function App, we can hit the URL with the below query string.
http://localhost:7071/api/HttpTriggerTest/?name=Gopal
5) Deploy the Azure Function
To deploy the Azure function, right-click on the HTTP function and select the Deploy to Function App option. Choose the resource group and deploy it.
Thanks for the reading. Please share your inputs in the comment section of this post.