Skip to content

Latest commit

 

History

History
103 lines (63 loc) · 6.66 KB

create-first-function-vs-code-python.md

File metadata and controls

103 lines (63 loc) · 6.66 KB
title description ms.topic ms.date ms.devlang ms.custom ai-usage
Create a Python function using Visual Studio Code - Azure Functions
Learn how to create a Python function, then publish the local project to serverless hosting in Azure Functions using the Azure Functions extension in Visual Studio Code.
quickstart
09/10/2024
python
devx-track-python, mode-api, devdivchpfy22, vscode-azure-extension-update-complete, ai-video-demo
ai-assisted

Quickstart: Create a function in Azure with Python using Visual Studio Code

In this article, you use Visual Studio Code to create a Python function that responds to HTTP requests. After testing the code locally, you deploy it to the serverless environment of Azure Functions.

This article uses the Python v2 programming model for Azure Functions, which provides a decorator-based approach for creating functions. To learn more about the Python v2 programming model, see the Developer Reference Guide

Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.

There's also a CLI-based version of this article.

This video shows you how to create a Python function in Azure using Visual Studio Code.

[!VIDEO a1e10f96-2940-489c-bc53-da2b915c8fc2]

The steps in the video are also described in the following sections.

Configure your environment

Before you begin, make sure that you have the following requirements in place:

[!INCLUDE functions-install-core-tools-vs-code]

Create your local project

In this section, you use Visual Studio Code to create a local Azure Functions project in Python. Later in this article, you publish your function code to Azure.

  1. In Visual Studio Code, press F1 to open the command palette and search for and run the command Azure Functions: Create New Project....

  2. Choose the directory location for your project workspace and choose Select. You should either create a new folder or choose an empty folder for the project workspace. Don't choose a project folder that is already part of a workspace.

  3. Provide the following information at the prompts:

    Prompt Selection
    Select a language Choose Python (Programming Model V2).
    Select a Python interpreter to create a virtual environment Choose your preferred Python interpreter. If an option isn't shown, type in the full path to your Python binary.
    Select a template for your project's first function Choose HTTP trigger.
    Name of the function you want to create Enter HttpExample.
    Authorization level Choose ANONYMOUS, which lets anyone call your function endpoint. For more information, see Authorization level.
    Select how you would like to open your project Choose Open in current window.
  4. Visual Studio Code uses the provided information and generates an Azure Functions project with an HTTP trigger. You can view the local project files in the Explorer. The generated function_app.py project file contains your functions.

  5. In the local.settings.json file, update the AzureWebJobsStorage setting as in the following example:

    "AzureWebJobsStorage": "UseDevelopmentStorage=true",

    This tells the local Functions host to use the storage emulator for the storage connection required by the Python v2 model. When you publish your project to Azure, this setting uses the default storage account instead. If you're using an Azure Storage account during local development, set your storage account connection string here.

Start the emulator

  1. In Visual Studio Code, press F1 to open the command palette. In the command palette, search for and select Azurite: Start.

  2. Check the bottom bar and verify that Azurite emulation services are running. If so, you can now run your function locally.

[!INCLUDE functions-run-function-test-local-vs-code]

After you verify that the function runs correctly on your local computer, it's time to use Visual Studio Code to publish the project directly to Azure.

[!INCLUDE functions-sign-in-vs-code]

[!INCLUDE functions-publish-project-vscode]

[!INCLUDE functions-vs-code-run-remote]

[!INCLUDE functions-cleanup-resources-vs-code.md]

Next steps

You created and deployed a function app with a simple HTTP-triggered function. In the next articles, you expand that function by connecting to a storage service in Azure. To learn more about connecting to other Azure services, see Add bindings to an existing function in Azure Functions.

[!div class="nextstepaction"] Connect to Azure Cosmos DB [!div class="nextstepaction"] Connect to an Azure Storage queue

Having issues? Let us know.