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.
Before you start, you must:
Install Python 3.7.4. This version of Python is verified with Functions. Python 3.8 and later versions are not yet supported.
Install Azure Functions Core Tools version 2.7.1846 or a later version.
Install the Azure CLI version 2.0.76 or a later version.
Have an active Azure subscription.
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
If Python didn't install venv on your Linux distribution, you can install it using the following command:
sudo apt-get install python3-venv
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
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
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
scriptFilepoints to the file containing the function, and the
bindingsarray defines the invocation trigger and bindings.
__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
binding parameter. The
req, which is defined in function.json, is an instance of the azure.functions.HttpRequest class.
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:
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
If you aren't 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. 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.
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>
Your Python project is built remotely in Azure from the files in the deployment package.
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.
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.
To view near real-time logs for a published Python app, use 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...