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.
Before you start, you must have the following:
Install Python 3.6.
Install Azure Functions Core Tools version 2.6.1071 or a later version.
Install the Azure CLI version 2.x or a later version.
An active Azure subscription.
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
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.jsonis used to store app settings and connection strings when running locally. This file doesn't get published to Azure.
requirements.txtcontains the list of packages to be installed on publishing to Azure.
host.jsoncontains global configuration options that affect all functions in a function app. This file does get published to Azure.
Navigate to the new MyFunctionProj folder:
Next, you update the host.json file to enable extension bundles.
Create a function
To add a function to your project, run the following command:
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
scriptFilepoints to the file containing the function, while the invocation trigger and bindings are defined in the
__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
reqnamed binding parameter. Defined in function.json,
reqis an instance of the azure.functions.HttpRequest class.
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/MyHttpTrigger [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:
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
If you are not using Cloud Shell, sign in first using
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
<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>
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>
func azure functionapp publish <APP_NAME> --build remote
npm run build:production func azure functionapp publish <APP_NAME>
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....
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 that you copied from the previous step, append the query string
&name=<yourname> to the URL, as in the following example:
You can also paste the copied URL in to the address of your web browser. Again, append the query string
&name=<yourname> to the URL before you execute the request.
To view near real-time logs for a published Python app, we recommend using the Application Insights Live Metrics Stream
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...