Functions
This guide shows you how to work with Azure Functions using Python and Azure Functions Core Tools. This lab demonstrates how to develop, run, debug, test, and validate an HTTP-triggered function locally.
Prerequisites
Section titled “Prerequisites”- Docker installed and running.
- Dev Containers extension installed (optional).
How to execute
Section titled “How to execute”-
Setup Environment:
Open VS Code in the project folder and execute this command in the Command Palette:
Terminal window > Dev Containers: Reopen in Container- Run the setup script to install tools and dependencies.
Terminal window scripts/setup.sh - Start Infrastructure: Launch the required containers.
Terminal window docker compose up -d
- Run the setup script to install tools and dependencies.
-
Start Function Host:
Terminal window func start- Open: Open the Run and Debug tab in VS Code.
- Run: Select Attach to Python Functions and press
F5. The emulator starts and the debugger attaches automatically.
- Open: Open the Azure sidebar and navigate to Workspace → Local Project → Functions.
- Run: Click the Start debugging button to start the function app.
-
Run the Example:
Terminal window python main.py- Admin (success):
Terminal window curl "http://localhost:7071/api/get_secret?username=admin" - Other user (forbidden):
Terminal window curl "http://localhost:7071/api/get_secret?username=guest"
- Install: REST Client from de VS Code Marketplace.
- Open: Open
http/get_secret.httpin VS Code. - Run: Click Send Request above any request block.
- Admin (success):
How to debug
Section titled “How to debug”-
function_app.py:
- Open: Open
function_app.py. - Breakpoints: Set breakpoints inside
get_secret. - Run: From the Run and Debug tab, select Attach to Python Functions and press
F5.
- Open: Open
-
main.py:
- Open: Open
main.py. - Breakpoints: Set breakpoints in the
mainfunction. - Run: Press
F5to start debugging.
- Open: Open
-
Tests:
- Open: Open a test file (e.g.,
tests/test_functions.py). - Breakpoints: Set breakpoints in the test code.
- Run: Use the VS Code Testing tab and click the Debug Test icon next to the test you want to debug.
- Open: Open a test file (e.g.,
How to test
Section titled “How to test”- Individually: Run tests individually from the VS Code Testing tab.
- All tests: Run the automated script:
Terminal window scripts/run_tests.sh
Validate results
Section titled “Validate results”- Run: Execute the main script and review its output:
Terminal window python main.py - Verify: The user defined in
ADMIN_USERNAME(.env) should receive a200response with the secret value. Any other user should receive403.
- Open: Review the terminal where
func startis running. - Verify: Each request logs a line like
Python HTTP trigger function processed a request.
Clean Up
Section titled “Clean Up”docker compose down -v