Skip to content

Latest commit

 

History

History
218 lines (130 loc) · 12 KB

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

File metadata and controls

218 lines (130 loc) · 12 KB
title description ms.topic ms.date ms.custom ROBOTS
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
11/04/2020
devx-track-python
NOINDEX,NOFOLLOW

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

[!div class="op_single_selector" title1="Select your function language: "]

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.

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.

1. Prepare your environment

Before you get started, make sure you have the following requirements in place:



2. Create your local project

  1. Choose the Azure icon in the Activity bar, then in the Azure: Functions area, select the Create new project... icon.

    Choose Create a new project

  2. Choose a directory location for your project workspace and choose Select.

    [!NOTE] These steps were designed to be completed outside of a workspace. In this case, do not select a project folder that is part of a workspace.

  3. Provide the following information at the prompts:

    • Select a language for your function project: Choose Python.

    • Select a Python alias to create a virtual environment: Choose the location of your Python interpreter. If the location isn't shown, type in the full path to your Python binary.

    • Select a template for your project's first function: Choose HTTP trigger.

    • Provide a function name: Type HttpExample.

    • Authorization level: Choose Anonymous, which enables anyone to call your function endpoint. To learn about authorization levels, see Authorization keys.

    • Select how you would like to open your project: Choose Add to workspace.


Can't create a function project?

The most common issues to resolve when creating a local Functions project are:

  • You do not have the Azure Functions extension installed.


Run the function locally

  1. Press F5 to start the function app project.

  2. In the Terminal panel, see the URL endpoint of your function running locally.

    Local function VS Code output

  3. With Core Tools running, go to the Azure: Functions area. Under Functions, expand Local Project > Functions. Right-click (Windows) or Ctrl - click (macOS) the HttpExample function and choose Execute Function Now....

    :::image type="content" source="../../includes/media/functions-run-function-test-local-vs-code/execute-function-now.png" alt-text="Execute function now from Visual Studio Code":::

  4. In Enter request body you see the request message body value of { "name": "Azure" }. Press Enter to send this request message to your function.

  5. When the function executes locally and returns a response, a notification is raised in Visual Studio Code. Information about the function execution is shown in Terminal panel.

  6. Press Ctrl + C to stop Core Tools and disconnect the debugger.


Can't run the function locally?

The most common issues to resolve when running a local Functions project are:

  • You do not have the Core Tools installed.
  • If you have trouble running on Windows, make sure that the default terminal shell for Visual Studio Code isn't set to WSL Bash.


4. Sign in to Azure

To publish your app, sign in to Azure. If you're already signed in, go to the next section.

  1. Choose the Azure icon in the Activity bar, then in the Azure: Functions area, choose Sign in to Azure....

    Sign in to Azure within VS Code

  2. When prompted in the browser, choose your Azure account and sign in using your Azure account credentials.

  3. After you've successfully signed in, close the new browser window and go back to Visual Studio Code.



5. Publish the project to Azure

Your first deployment of your code includes creating a Function resource in your Azure subscription.

  1. Choose the Azure icon in the Activity bar, then in the Azure: Functions area, choose the Deploy to function app... button.

    Publish your project to Azure

  2. Provide the following information at the prompts:

    • Select folder: Choose the folder that contains your function app.

    • Select subscription: Choose the subscription to use. You won't see this if you only have one subscription.

    • Select Function App in Azure: Choose + Create new Function App.

    • Enter a globally unique name for the function app: Type a name that is valid in a URL path. The name you type is validated to make sure that it's unique across Azure.

    • Select a runtime: Choose the version of Python you've been running on locally. You can use the python --version command to check your version.

    • Select a location for new resources: For better performance, choose a region near you.

    The extension shows the status of individual resources as they are being created in Azure in the notification area.

    :::image type="content" source="../../includes/media/functions-publish-project-vscode/resource-notification.png" alt-text="Notification of Azure resource creation":::

  3. A notification is displayed after your function app is created and the deployment package is applied. Select View Output to view the creation and deployment results.

    Create complete notification


Can't publish the function?

This section created the Azure resources and deployed your local code to the Function app. If that didn't succeed:

  • Review the Output for error information. The bell icon in the lower right corner is another way to view the output.
  • Did you publish to an existing function app? That action overwrites the content of that app in Azure.

What resources were created?

When completed, the following Azure resources are created in your subscription, using names based on your function app name:

  • Resource group: A resource group is a logical container for related resources in the same region.
  • Azure Storage account: A Storage resource maintains state and other information about your project.
  • Consumption plan: A consumption plan defines the underlying host for your serverless function app.
  • Function app: A function app provides the environment for executing your function code and group functions as a logical unit.
  • Application Insights: Application Insights tracks usage of your serverless function.


6. Run the function in Azure

  1. Back in the Azure: Functions side bar, expand the new function app.

  2. Expand Functions, then right-click (Windows) or Ctrl - click (macOS) the HttpExample function and choose Execute Function Now....

    :::image type="content" source="../../includes/media/functions-vs-code-run-remote/execute-function-now.png" alt-text="Execute function now in Azure from Visual Studio Code":::

  3. In Enter request body you see the request message body value of { "name": "Azure" }.

    Press Enter to send this request message to your function.

  4. When the function executes in Azure and returns a response, a notification is raised in Visual Studio Code.

7. Clean up resources

When you continue to the next step and add an Azure Storage queue output binding to your function, you'll need to keep all your resources in place to build on what you've already done.

Otherwise, you can use the following steps to delete the function app and its related resources to avoid incurring any further costs.

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

To learn more about Functions costs, see Estimating Consumption plan costs.

Next steps

Expand that function by adding an output binding. This binding writes the string from the HTTP request to a message in an Azure Queue Storage queue.

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

Having issues? Let us know.