Quickstart: Create an HTTP triggered Python function in Azure

This article shows you how to use command-line tools to create a Python project that runs in Azure Functions. You also create a function that is triggered by an HTTP request. After running locally, you publish your project to run as a serverless function in Azure.

This article is the first of two Python quickstarts for Azure Functions. After you complete this quickstart, you can add an Azure Storage queue output binding to your function.

There is also a Visual Studio Code-based version of this article.

Prerequisites

Before you start, you must:

Create and activate a virtual environment

You should use a Python 3.7 environment to locally develop Python functions. Run the following commands to create and activate a virtual environment named .venv.

Note

If Python didn't install venv on your Linux distribution, you can install it using the following command:

sudo apt-get install python3-venv

Bash:

python -m venv .venv
source .venv/bin/activate

PowerShell or a Windows command prompt:

py -m venv .venv
.venv\scripts\activate

Now that you activated the virtual environment, run the remaining commands in it. To get out of the virtual environment, run deactivate.

Create a local functions project

A functions project can have multiple functions that all share the same local and hosting configurations.

In the virtual environment, run the following commands:

func init MyFunctionProj --python
cd MyFunctionProj

The func init command creates a MyFunctionProj folder. The Python project in this folder doesn't yet have any functions. You'll add them next.

Create a function

To add a function to your project, run the following command:

func new --name HttpTrigger --template "HTTP trigger"

This commands creates a subfolder named HttpTrigger, which contains the following files:

  • function.json: configuration file that defines the function, trigger, and other bindings. Notice that in this file, the value for scriptFile points to the file containing the function, and the bindings array defines the invocation trigger and bindings.

    Each binding requires a direction, type and a unique name. The HTTP trigger has an input binding of type httpTrigger and output binding of type http.

  • __init__.py: script file that is your HTTP triggered function. Notice that this script has a default main(). HTTP data from the trigger passes to the function using the req named binding parameter. The req, which is defined in function.json, is an instance of the azure.functions.HttpRequest class.

    The return object, defined as $return in function.json, is an instance of azure.functions.HttpResponse class. To learn more, see Azure Functions HTTP triggers and bindings.

Now you can run the new function on your local computer.

Run the function locally

This command starts the function app using the Azure Functions runtime (func.exe):

func host start

You should see the following information written to the output:

Http Functions:

        HttpTrigger: http://localhost:7071/api/HttpTrigger    

Copy the URL of your HttpTrigger function from this output and paste it into your browser's address bar. Append the query string ?name=<yourname> to this URL and execute the request. The following screenshot shows the response to the GET request that the local function returns to the browser:

Verify locally in the browser

Use Ctrl+C to shut down your function app execution.

Now that you have run your function locally, you can deploy your function code to Azure.
Before you can deploy your app, you'll need to create some Azure resources.

Create a resource group

Create a resource group with the az group create command. An Azure resource group is a logical container into which Azure resources like function apps, databases, and storage accounts are deployed and managed.

The following example creates a resource group named myResourceGroup.
If you aren't using Cloud Shell, sign in first using az login.

az group create --name myResourceGroup --location westeurope

You generally create your resource group and the resources in a region near you.

Create an Azure Storage account

Functions uses a general-purpose account in Azure Storage to maintain state and other information about your functions. Create a general-purpose storage account in the resource group you created by using the az storage account create command.

In the following command, substitute a globally unique storage account name where you see the <storage_name> placeholder. Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only.

az storage account create --name <storage_name> --location westeurope --resource-group myResourceGroup --sku Standard_LRS

Create a function app in Azure

A function app provides an environment for executing your function code. It lets you group functions as a logical unit for easier management, deployment, and sharing of resources.

Run the following command. Replace <APP_NAME> with a unique function app name. Replace <STORAGE_NAME> with a storage account name. The <APP_NAME> is also the default DNS domain for the function app. This name needs to be unique across all apps in Azure.

Note

You can't host Linux and Windows apps in the same resource group. If you have an existing resource group named myResourceGroup with a Windows function app or web app, you must use a different resource group.

az functionapp create --resource-group myResourceGroup --os-type Linux \
--consumption-plan-location westeurope  --runtime python --runtime-version 3.7 \
--name <APP_NAME> --storage-account  <STORAGE_NAME>

The preceding command creates a function app running Python 3.7.4. It also provisions an associated Azure Application Insights instance in the same resource group. You can use this instance to monitor your function app and view logs.

You're now ready to publish your local functions project to the function app in Azure.

Deploy the function app project to Azure

After you create the function app in Azure, you can use the func azure functionapp publish Core Tools command to deploy your project code to Azure. In this example, replace <APP_NAME> with the name of your app.

func azure functionapp publish <APP_NAME> --build remote

The --build remote option builds your Python project remotely in Azure from the files in the deployment package, which is recommended.

You'll see output similar to the following message. It's truncated here so you can read it better:

Getting site publishing info...
...

Preparing archive...
Uploading content...
Upload completed successfully.
Deployment completed successfully.
Syncing triggers...
Functions in myfunctionapp:
    HttpTrigger - [httpTrigger]
        Invoke url: https://myfunctionapp.azurewebsites.net/api/httptrigger?code=cCr8sAxfBiow548FBDLS1....

You can copy the Invoke url value for your HttpTrigger and use it to verify your function in Azure. The URL contains a code query string value that is your function key, which makes it difficult for others to call your HTTP trigger endpoint in Azure.

Verify the function in Azure

Use cURL to verify the deployed function. Using the URL, including the function key, that you copied from the previous step, append the query string &name=<yourname> to the URL.

using cURL to call the function in Azure.

You can also paste the copied URL, including the function key, into the address bar of your web browser. Again, append the query string &name=<yourname> to the URL before you execute the request.

Using a web browser to call the function.

Note

To view near real-time logs for a published Python app, use the Application Insights Live Metrics Stream.

Next steps

You've created a Python functions project with an HTTP triggered function, run it on your local machine, and deployed it to Azure. Now, extend your function by...