GitHub - Azure-Samples/functions-quickstart-python-http-azd: This Quickstart uses Azure Developer command-line (azd) tools to create functions that respond to HTTP requests. After testing the code locally, you deploy it to a new serverless function app you create running in a Flex Consumption plan in Azure Functions. This follows current best practices for secure and scalable Azure Functions deployments (original) (raw)
Azure Functions Python HTTP Trigger using Azure Developer CLI
This template repository contains an HTTP trigger reference sample for Azure Functions written in Python and deployed to Azure using the Azure Developer CLI (azd
). The sample uses managed identity and a virtual network to make sure deployment is secure by default.
This source code supports the article Quickstart: Create and deploy functions to Azure Functions using the Azure Developer CLI.
Prerequisites
- Python 3.11
- Azure Functions Core Tools
- Azure Developer CLI (AZD)
- To use Visual Studio Code to run and debug locally:
Initialize the local project
You can initialize a project from this azd
template in one of these ways:
- Use this
azd init
command from an empty local (root) folder:
azd init --template functions-quickstart-python-http-azd
Supply an environment name, such asflexquickstart
when prompted. Inazd
, the environment is used to maintain a unique deployment context for your app. - Clone the GitHub template repository locally using the
git clone
command:
git clone https://github.com/Azure-Samples/functions-quickstart-python-http-azd.git
cd functions-quickstart-python-azd
You can also clone the repository from your own fork in GitHub.
Prepare your local environment
Add a file named local.settings.json
in the root of your project with the following contents:
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "python" } }
Create a virtual environment
The way that you create your virtual environment depends on your operating system. Open the terminal, navigate to the project folder, and run these commands:
Linux/macOS/bash
python -m venv .venv source .venv/bin/activate
Windows (Cmd)
py -m venv .venv .venv\scripts\activate
Run your app from the terminal
- To start the Functions host locally, run these commands in the virtual environment:
pip3 install -r requirements.txt
func start - From your HTTP test tool in a new terminal (or from your browser), call the HTTP GET endpoint: http://localhost:7071/api/httpget
- Test the HTTP POST trigger with a payload using your favorite secure HTTP test tool. This example uses the
curl
tool with payload data from the testdata.json project file:
curl -i http://localhost:7071/api/httppost -H "Content-Type: text/json" -d @testdata.json - When you're done, press Ctrl+C in the terminal window to stop the
func.exe
host process. - Run
deactivate
to shut down the virtual environment.
Run your app using Visual Studio Code
- Open the root folder in a new terminal.
- Run the
code .
code command to open the project in Visual Studio Code. - Press Run/Debug (F5) to run in the debugger. Select Debug anyway if prompted about local emulator not running.
- Send GET and POST requests to the
httpget
andhttppost
endpoints respectively using your HTTP test tool (or browser forhttpget
). If you have the RestClient extension installed, you can execute requests directly from the test.http project file.
Source Code
The source code for both functions is in the function_app.py code file. Azure Functions requires the use of the @azure/functions
library.
This code shows an HTTP GET triggered function:
@app.route(route="httpget", methods=["GET"]) def http_get(req: func.HttpRequest) -> func.HttpResponse: name = req.params.get("name", "World")
logging.info(f"Processing GET request. Name: {name}")
return func.HttpResponse(f"Hello, {name}!")
This code shows an HTTP POST triggered function:
@app.route(route="httppost", methods=["POST"]) def http_post(req: func.HttpRequest) -> func.HttpResponse: try: req_body = req.get_json() name = req_body.get('name') age = req_body.get('age')
logging.info(f"Processing POST request. Name: {name}")
if name and isinstance(name, str) and age and isinstance(age, int):
return func.HttpResponse(f"Hello, {name}! You are {age} years old!")
else:
return func.HttpResponse(
"Please provide both 'name' and 'age' in the request body.",
status_code=400
)
except ValueError:
return func.HttpResponse(
"Invalid JSON in request body",
status_code=400
)
Deploy to Azure
Run this command to provision the function app, with any required Azure resources, and deploy your code:
By default, this sample deploys with a virtual network (VNet) for enhanced security, ensuring that the function app and related resources are isolated within a private network. The VNET_ENABLED
parameter controls whether a VNet is used during deployment:
- When
VNET_ENABLED
istrue
(default), the function app is deployed with a VNet for secure communication and resource isolation. - When
VNET_ENABLED
isfalse
, the function app is deployed without a VNet, allowing public access to resources.
This parameter replaces the previous SKIP_VNET
parameter. If you were using SKIP_VNET
in earlier versions, set VNET_ENABLED
to false
to achieve the same behavior.
To disable the VNet for this sample, set VNET_ENABLED
to false
before running azd up
:
azd env set VNET_ENABLED false azd up
You're prompted to supply these required deployment parameters:
Parameter | Description |
---|---|
Environment name | An environment that's used to maintain a unique deployment context for your app. You aren't prompted when you created the local project using azd init. |
Azure subscription | Subscription in which your resources are created. |
Azure location | Azure region in which to create the resource group that contains the new Azure resources. Only regions that currently support the Flex Consumption plan are shown. |
To learn how to obtain your new function endpoints in Azure along with the required function keys, see Invoke the function on Azure in the companion article Quickstart: Create and deploy functions to Azure Functions using the Azure Developer CLI.
Redeploy your code
You can run the azd up
command as many times as you need to both provision your Azure resources and deploy code updates to your function app.
Note
Deployed code files are always overwritten by the latest deployment package.
Clean up resources
When you're done working with your function app and related resources, you can use this command to delete the function app and its related resources from Azure and avoid incurring any further costs: