Create an HTTP triggered function in Azure

This article shows you how to use command-line tools to create a Python project that runs in Azure Functions. The function you create is triggered by HTTP requests. Finally, you publish your project to run as a serverless function in Azure.

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

Prerequisites

Before you start, you must have the following:

If you don't have an Azure subscription, create a free account before you begin.

Create and activate a virtual environment (optional)

To locally develop and test Python functions, it is recommended to use a Python 3.6 environment. 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:

python3.6 -m venv .venv
source .venv/bin/activate

PowerShell or a Windows command prompt:

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

The remaining commands are run inside the virtual environment.

Create a local Functions project

A Functions project is the equivalent of a function app in Azure. It can have multiple functions that all share the same local and hosting configurations.

In the virtual environment, run the following command, choosing python as your worker runtime.

func init MyFunctionProj

A folder named MyFunctionProj is created, which contains the following three files:

  • local.settings.json is used to store app settings and connection strings when running locally. This file doesn't get published to Azure.
  • requirements.txt contains the list of packages to be installed on publishing to Azure.
  • host.json contains global configuration options that affect all functions in a function app. This file does get published to Azure.

Navigate to the new MyFunctionProj folder:

cd MyFunctionProj

Create a function

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

func new

Choose the HTTP trigger template, type HttpTrigger as the name for the function, then press Enter.

A subfolder named HttpTrigger is created, which contains the following files:

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

    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. Review this script and see that it contains a default main(). HTTP data from the trigger is passed to this function using the req named binding parameter. Defined in function.json, req 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.

Run the function locally

The following command starts the function app, which runs locally using the same Azure Functions runtime that is in Azure.

func host start

When the Functions host starts, it writes something like the following output, which has been truncated for readability:


                  %%%%%%
                 %%%%%%
            @   %%%%%%    @
          @@   %%%%%%      @@
       @@@    %%%%%%%%%%%    @@@
     @@      %%%%%%%%%%        @@
       @@         %%%%       @@
         @@      %%%       @@
           @@    %%      @@
                %%
                %

...

Content root path: C:\functions\MyFunctionProj
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.

...

Http Functions:

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

[8/27/2018 10:38:27 PM] Host started (29486ms)
[8/27/2018 10:38:27 PM] Job host started

Copy the URL of your HttpTrigger function from the runtime 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 shows the response in the browser to the GET request returned by the local function:

Test locally in the browser

Now that you have run your function locally, you can create the function app and other required resources in Azure.

Create a resource group

Create a resource group with the az group create. 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 are not 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 using a unique function app name in place of the <APP_NAME> placeholder and the storage account name for <STORAGE_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.

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

Note

Linux and Windows apps cannot be hosted 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.

This command will also provision an associated Azure Application Insights instance in the same resource group that can be used for monitoring and viewing 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 the function app is created in Azure, you can use the func azure functionapp publish Core Tools command to deploy your project code to Azure. In these examples, replace <APP_NAME> with the name of your app from the previous step.

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.

You'll see output similar to the following, which has been truncated for readability:

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....

Copy the Invoke url value for your HttpTrigger, which you can now use to test your function in Azure. The URL contains a code query string value that is your function key. This key makes it difficult for others to call your HTTP trigger endpoint in Azure.

Test the function in Azure

Use cURL to test 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, in to the address 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, we recommend using 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...