On this page
article
Azure Functions with Python
Build and deploy Python functions on Azure Functions — HTTP triggers, blob storage events, and Azure integration.
Azure Functions runs Python code on Microsoft’s serverless platform, integrating with Azure services, Cosmos DB, and Event Grid.
Setup
Install the Azure Functions Core Tools and Azure CLI:
# macOS
brew tap azure/functions
brew install azure-functions-core-tools@4
# Verify
func --version
az --version
Create a Function App
func init MyFunctionApp --python
cd MyFunctionApp
func new --name HttpTrigger --template "HTTP trigger" --authlevel anonymous
This creates:
MyFunctionApp/
├── function_app.py
├── host.json
├── local.settings.json
└── requirements.txt
HTTP Trigger Function
# function_app.py
import azure.functions as func
import json
app = func.FunctionApp()
@app.route(route="hello", auth_level=func.AuthLevel.ANONYMOUS)
def hello(req: func.HttpRequest) -> func.HttpResponse:
name = req.params.get('name', 'World')
return func.HttpResponse(f"Hello, {name}!")
@app.route(route="items", methods=["GET", "POST"])
def items(req: func.HttpRequest) -> func.HttpResponse:
if req.method == "GET":
return func.HttpResponse(
json.dumps([{"id": 1, "name": "Item 1"}]),
mimetype="application/json",
)
body = req.get_json()
return func.HttpResponse(
json.dumps({"id": 2, "name": body["name"]}),
status_code=201,
mimetype="application/json",
)
Local Development
# Create virtual environment
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
# Run locally
func start
# Visit http://localhost:7071/api/hello?name=Alice
Deploy to Azure
# Login
az login
# Create resource group and storage account
az group create --name MyResourceGroup --location eastus
az storage account create --name mystorageaccount --location eastus --resource-group MyResourceGroup --sku Standard_LRS
# Create function app
az functionapp create \
--resource-group MyResourceGroup \
--consumption-plan-location eastus \
--runtime python \
--runtime-version 3.12 \
--functions-version 4 \
--name my-python-function-app \
--storage-account mystorageaccount
# Deploy
func azure functionapp publish my-python-function-app
Blob Storage Trigger
Process files uploaded to Azure Blob Storage:
@app.blob_trigger(arg_name="myblob", path="uploads/{name}", connection="AzureWebJobsStorage")
def blob_processor(myblob: func.InputStream):
print(f"Processing blob: {myblob.name}, Size: {myblob.length} bytes")
content = myblob.read().decode('utf-8')
# Process content...
Timer Trigger
Run on a schedule (cron expression):
@app.timer_trigger(schedule="0 0 8 * * *", arg_name="timer", run_on_startup=False)
def daily_report(timer: func.TimerRequest) -> None:
print("Generating daily report...")
# Generate and send report...
Application Settings
Store configuration in local.settings.json (local) or Azure App Settings (production):
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "python",
"DATABASE_URL": "your-connection-string"
}
}
Access in code:
import os
db_url = os.environ["DATABASE_URL"]
Best Practices
- Use the v2 programming model (
function_app.pywith decorators) - Store secrets in Azure Key Vault, not in code or settings files
- Use Application Insights for monitoring and debugging
- Set appropriate timeout and memory in
host.json - Use Durable Functions for complex orchestration workflows
Azure Functions is the natural choice when your infrastructure runs on Microsoft Azure.