This template repository contains a timer trigger reference sample for functions written in Python v2 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. You can opt out of a VNet being used in the sample by setting VNET_ENABLED to false in the parameters.
This project is designed to run on your local computer. You can also use GitHub Codespaces:
This codespace is already configured with the required tools to complete this tutorial using either azd or Visual Studio Code. If you're working a codespace, skip down to Run your app section.
- Regular data processing: Schedule batch processing jobs to run at specific intervals
- Maintenance tasks: Perform periodic cleanup or maintenance operations on your data
- Scheduled notifications: Send automated reports or alerts on a fixed schedule
- Integration polling: Regularly check for updates in external systems that don't support push notifications
- Azure Storage Emulator (Azurite) - Required for local development with Azure Functions
- Python 3.11
- Azure Functions Core Tools
- Azure Developer CLI
- To use Visual Studio Code to run and debug locally:
You can initialize a project from this azd template in one of these ways:
-
Use this
azd initcommand from an empty local (root) folder:azd init --template functions-quickstart-python-azd-timer
Supply an environment name, such as
flexquickstartwhen prompted. Inazd, the environment is used to maintain a unique deployment context for your app. -
Clone the GitHub template repository locally using the
git clonecommand:git clone https://github.com/Azure-Samples/functions-quickstart-python-azd-timer.git cd functions-quickstart-python-azd-timerYou can also clone the repository from your own fork in GitHub.
Add a file named local.settings.json in the src folder with the following contents:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "python",
"TIMER_SCHEDULE": "*/30 * * * * *"
}
}The TIMER_SCHEDULE setting defines when your timer function runs using NCRONTAB format. The example above runs every 30 seconds. For more information on NCRONTAB expressions, see Configuration.
-
Start Azurite storage emulator in a separate terminal window:
docker run -p 10000:10000 -p 10001:10001 -p 10002:10002 mcr.microsoft.com/azure-storage/azurite
-
From the
srcfolder, run this command to start the Functions host locally:cd src func start -
Wait for the timer schedule to execute the timer trigger.
-
When you're done, press Ctrl+C in the terminal window to stop the
func.exehost process.
- Open the
srcapp folder in a new terminal. - Run the
code .command to open the project in Visual Studio Code. - In the command palette (F1), type
Azurite: Start, which enables debugging without warnings. - Press Run/Debug (F5) to run in the debugger. Select Debug anyway if prompted about local emulator not running.
- Wait for the timer schedule to trigger your timer function.
Run this command to provision the function app, with any required Azure resources, and deploy your code:
azd upAlternatively, you can opt-out of a VNet being used in the sample. To do so, use azd env to configure VNET_ENABLED to false before running azd up:
azd env set VNET_ENABLED false
azd upYou 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.
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:
azd downThe function code for the timer trigger is defined in src/function_app.py.
This code shows the timer function implementation:
import datetime
import logging
import azure.functions as func
# Create the function app instance
app = func.FunctionApp()
@app.timer_trigger(schedule="%TIMER_SCHEDULE%",
arg_name="mytimer",
run_on_startup=True,
use_monitor=False)
def timer_function(mytimer: func.TimerRequest) -> None:
"""
Timer-triggered function that executes on a schedule defined by TIMER_SCHEDULE app setting.
Args:
mytimer: Timer information including schedule status
Notes:
The run_on_startup=True parameter is useful for development and testing as it triggers
the function immediately when the host starts, but should typically be set to False
in production to avoid unexpected executions during deployments or restarts.
"""
utc_timestamp = datetime.datetime.now(datetime.timezone.utc).isoformat()
logging.info(f'Python timer trigger function executed at: {utc_timestamp}')
if mytimer.past_due:
logging.warning('The timer is running late!')The Python v2 programming model uses the @app.timer_trigger decorator from azure-functions to define the function
-
Parameterized Schedule: The function uses the
%TIMER_SCHEDULE%environment variable to determine the execution schedule, making it configurable without code changes. -
run_on_startup Parameter: Setting
run_on_startup=Truemakes the function execute immediately when the app starts, in addition to running on the defined schedule. This is useful for testing but should be set toFalsein production. -
Past Due Detection: The function checks if the timer is past due using the
mytimer.past_dueproperty, allowing for appropriate handling of delayed executions. -
Python v2 Programming Model: This function uses the Python v2 programming model with decorators, providing a more intuitive and Pythonic development experience.
-
Built-in Logging: The function uses Python's built-in logging module, which integrates seamlessly with Azure Functions and Application Insights.
The timer schedule is configured through the TIMER_SCHEDULE application setting, which follows the NCRONTAB expression format. For example:
0 */5 * * * *- Run once every 5 minutes0 0 */1 * * *- Run once every hour0 0 0 * * *- Run once every day at midnight
For more information on NCRONTAB expressions, see Timer trigger for Azure Functions.