This guide provides a sample Python Azure Function that is triggered by an HTTP request. Learn how to create, deploy, and test a simple HTTP-triggered function in Azure.
Overview
Azure Functions allows you to run small pieces of code, or "functions," in the cloud without needing to manage infrastructure. The HTTP trigger allows your function to be invoked by an HTTP request. This is a common pattern for building APIs, webhooks, and other serverless applications.
Prerequisites
- An Azure subscription. If you don't have one, create a free account.
- Python 3.7 or later installed.
- Azure Functions Core Tools installed.
- Visual Studio Code with the Azure Functions extension (recommended).
Creating the Function
You can create an HTTP-triggered function using the Azure Functions Core Tools or Visual Studio Code.
Using Azure Functions Core Tools
- Open a terminal or command prompt.
- Create a new function project:
func init MyHttpFunction --python - Navigate into the project directory:
cd MyHttpFunction - Create a new HTTP-triggered function:
func new --name HttpTriggerFunction --template "HTTP trigger"
Using Visual Studio Code
- Open Visual Studio Code.
- Click on the Azure icon in the Activity Bar.
- Under "Azure Functions," click "Create New Project...".
- Select a folder for your project.
- Select "Python".
- Select the Python interpreter.
- Choose the template "HTTP trigger".
- Provide a name for your function (e.g.,
HttpTriggerFunction). - Choose an authorization level (e.g., "Function").
Sample Code
The generated __init__.py file for an HTTP-triggered function typically looks like this:
HttpTriggerFunction/__init__.py
import logging
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(
f"Hello, {name}. This HTTP triggered function executed successfully."
)
else:
return func.HttpResponse(
"Please pass a name on the query string or in the request body",
status_code=400
)
The function.json file defines the function's bindings:
HttpTriggerFunction/function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
Testing the Function Locally
To test your function locally, use the Azure Functions Core Tools:
- In your project directory, run
func start.
The tools will provide you with a URL to test your function. You can send a GET request with a query parameter like ?name=YourName or a POST request with a JSON body like {"name": "YourName"}.
Deploying to Azure
You can deploy your function to Azure using the Azure CLI, Azure Functions Core Tools, or Visual Studio Code.
Using Azure CLI
- Login to Azure:
az login - Create a Resource Group (if you don't have one):
az group create --name MyResourceGroup --location westus - Create a Storage Account:
az storage account create --name mystorageaccount --location westus --resource-group MyResourceGroup --sku Standard_LRS - Create an Azure Function App:
az functionapp create --resource-group MyResourceGroup --consumption-plan-location westus --runtime python --runtime-version 3.7 --functions-version 3 --name MyPythonFunctionApp --storage-account mystorageaccount - Deploy your function code:
func azure functionapp publish MyPythonFunctionApp
Related Documentation
| Resource | Description |
|---|---|
| Azure Functions Overview | Learn about the serverless compute service for building and running event-driven applications. |
| Python Developer Guide for Azure Functions | Detailed information on developing Azure Functions using Python. |
| HTTP Trigger Binding | Understanding the configuration and behavior of the HTTP trigger. |