Create Python Azure function in Visual Studio Code

Azure Functions is a serverless computing service that enables developers to deploy small pieces of code without the need to manage the underlying infrastructure. It supports various programming languages, including .NET, Python, JavaScript, TypeScript, Java, PowerShell, and C#, providing flexibility and versatility for various applications. This article describes how to create Python Azure function in Visual Studio code.

Prerequisites:
1. Visual Studio Code installed on your machine.
2.Azure Functions Core Tools installed. This can be installed via Node.js or the npm install -g azure-functions-core-tools command.
3. Python Interpreter (Ensure Python 3.8 or later is installed, preferably the latest version).Azure Account with an active subscription. If you don’t have one, create a free Azure account at Azure Free Account.
4. Azure Functions Extension for Visual Studio Code installed. Install this from the Extensions marketplace in Visual Studio Code.
5. Azure CLI (Command Line Interface), for deploying the function.

create python azure function

Follow the below steps to Create Python Azure function in Visual Studio Code

1. Open Visual Studio Code and on the left-hand side ribbon, Click Azure Icon. A menu at the bottom will pop up. Click on the Azure function icon and click the ‘Create Function’ menu item.

2. Select Python as a language as shown in the below image.

3. Select ‘Model V2’ as the Python programming model.

4. Select Python interpreter as the latest available version (3.11.9).

5. Select the type of trigger you want to use for this function. In our case, we will use the Time trigger. Provide a CRON Expression (e.g., 0 */5 * * * * for every 5 minutes) to schedule the timer trigger.

create python azure function

6. Once the above steps are complete, Visual Studio Code will generate a Python Azure Function template with a pre-defined structure. Open the __init__.py file and add the desired logic for your function.

import datetime
import logging

import azure.functions as func

def main(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
        logging.warning('The timer is past due!')

    logging.info(f'Python timer trigger function ran at {utc_timestamp}')

Debugging Your Function Locally

  1. Press F5 or click on the Run and Debug option in Visual Studio Code.
  2. Azure Core Tools will emulate the function runtime locally, allowing you to test the functionality.

Deploying the Function to Azure

  1. Click on the Azure Icon in Visual Studio Code.
  2. Sign in to your Azure account if not already done.
  3. Right-click on the Functions node and select Deploy to Function App.
  4. Follow the prompts to create or choose an Azure Function App in your subscription.

We have seen how to create Python Azure function in Visual Studio Code.

See more

Kunal Rathi

With over 13 years of experience in data engineering and analytics, I've assisted countless clients in gaining valuable insights from their data. As a dedicated supporter of Data, Cloud and DevOps, I'm excited to connect with individuals who share my passion for this field. If my work resonates with you, we can talk and collaborate.

Shopping Cart
Scroll to Top