Python developers can use Azure Functions to create lightweight, scalable, and efficient serverless applications. In this post, we will focus on triggers.
What Are Triggers in Azure Functions?
Triggers are the foundation of Azure Functions. They determine how a function is invoked. Each function must have exactly one trigger, and the trigger type dictates the data payload available to the function. Azure supports various triggers, including:
1. HTTP Trigger
- Allows functions to be invoked via HTTP requests.
- Useful for building APIs or responding to webhooks.
- Example:
import azure.functions as func
import datetime
import json
import logging
app = func.FunctionApp()
@app.route(route="http_trigger", auth_level=func.AuthLevel.ANONYMOUS)
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
return func.HttpResponse("Hello world from HTTP trigger")
Parameters:
-
route: Specifies the URL path to which the HTTP trigger will respond. In this case, the function is accessible at
<your_function_app_url>/api/http_trigger
. -
auth_level: Determines the authentication level for the function. Options include:
-
ANONYMOUS:
No authentication required. -
FUNCTION:
Requires a function-specific key. -
ADMIN:
Requires an admin-level key.
-
2. Timer Trigger
- Executes functions based on a schedule.
- Cron expressions are used for scheduling.
- Example:
import azure.functions as func
import datetime
import json
import logging
app = func.FunctionApp()
@app.timer_trigger(schedule="0 */5 * * * *", arg_name="myTimer", run_on_startup=False, use_monitor=False)
def timer_trigger(myTimer: func.TimerRequest) -> None:
if myTimer.past_due:
logging.info('The timer is past due!')
logging.info('Python timer trigger function executed.')
Parameters:
-
schedule: Defines the schedule using a CRON expression. Here,
0 */5 * * * *
specifies the function runs every 5 minutes starting at the 0th second. -
arg_name: The name of the argument passed to the function, representing the
TimerRequest
object. -
run_on_startup: If set to
True
, the function executes immediately when the app starts. Default is False. -
use_monitor: Determines if Azure should monitor for missed schedule executions. If
True
, Azure ensures missed executions are retried. Default isTrue
. In this example, it is set toFalse
.
3. Blob Trigger
- Responds to changes in Azure Blob Storage (e.g., file uploads).
- Example:
import azure.functions as func
import datetime
import json
import logging
app = func.FunctionApp()
@app.blob_trigger(arg_name="myblob", path="blobname", connection="BlobStorageConnectionString")
def BlobTrigger(myblob: func.InputStream):
logging.info(f"Python blob trigger function processed blob"
f"Name: {myblob.name}"
f"Blob Size: {myblob.length} bytes")
Parameters:
-
arg_name: Specifies the name of the argument in the function that represents the blob data. Here it is
myblob
. -
path: The path in the Blob Storage container that the function listens to. In this example, it is
blobname
. -
connection: Refers to the name of the application setting containing the connection string for the Blob Storage account. Here it is
BlobStorageConnectionString
.
4. Queue Trigger
- Triggered by messages added to Azure Storage Queues.
- Example:
import azure.functions as func
import datetime
import json
import logging
app = func.FunctionApp()
@app.queue_trigger(arg_name="azqueue", queue_name="queuename", connection="QueueConnectionString")
def queue_trigger(azqueue: func.QueueMessage):
logging.info('Python Queue trigger processed a message: %s',
azqueue.get_body().decode('utf-8'))
Parameters:
-
arg_name: Specifies the name of the argument that represents the queue message in the function. Here, it is
azqueue
. -
queue_name: The name of the Azure Storage Queue that the function listens to. In this case, it is
queuename
. -
connection: Refers to the application setting containing the connection string for the Azure Storage Queue. Here, it is
QueueConnectionString
.
5. EventHub Trigger
- Triggered by events sent to an Azure Event Hub.
- Example:
import azure.functions as func
import datetime
import json
import logging
app = func.FunctionApp()
@app.event_hub_message_trigger(arg_name="azeventhub", event_hub_name="eventhubname",
connection="EventHubConnectionString")
def eventhub_trigger(azeventhub: func.EventHubEvent):
logging.info('Python EventHub trigger processed an event: %s',
azeventhub.get_body().decode('utf-8'))
Parameters:
-
arg_name: This specifies the name of the parameter that will receive the event data in your function. In this case,
azeventhub
will be the variable representing the incomingEventHubEvent
. -
event_hub_name: This denotes the name of the Event Hub that the function is listening to. Replace
eventhubname
with the actual name of your Event Hub. -
connection: This refers to the name of the application setting that contains the connection string for the Event Hub. Ensure that your Azure Function App's settings include an entry named
EventHubConnectionString
with the appropriate connection string value.
6. ServiceBus Queue Trigger
- Triggered by messages added to an Azure Service Bus queue.
- Example:
import azure.functions as func
import datetime
import json
import logging
app = func.FunctionApp()
@app.service_bus_queue_trigger(arg_name="azservicebus", queue_name="servicebusqueuename",
connection="ServiceBusConnectionString")
def servicebus_trigger(azservicebus: func.ServiceBusMessage):
logging.info('Python ServiceBus Queue trigger processed a message: %s',
azservicebus.get_body().decode('utf-8'))
Parameters:
-
arg_name: This specifies the name of the parameter that will receive the message data in your function. In this case,
azservicebus
will be the variable representing the incomingServiceBusMessage
. -
queue_name: This denotes the name of the Service Bus queue that the function is listening to. Replace
servicebusqueuename
with the actual name of your Service Bus queue. -
connection: This refers to the name of the application setting that contains the connection string for the Service Bus. Ensure that your Azure Function App's settings include an entry named
ServiceBusConnectionString
with the appropriate connection string value.
7. ServiceBus Topic Trigger
- Triggered by messages published to an Azure Service Bus topic.
- Example:
import azure.functions as func
import datetime
import json
import logging
app = func.FunctionApp()
@app.service_bus_topic_trigger(arg_name="azservicebus", subscription_name="servicebussubscriptionName", topic_name="servicebustopicname",
connection="ServiceBusConnectionString")
def servicebus_topic_trigger(azservicebus: func.ServiceBusMessage):
logging.info('Python ServiceBus Topic trigger processed a message: %s',
azservicebus.get_body().decode('utf-8'))
Parameters:
-
arg_name: Specifies the name of the argument that represents the Service Bus message in the function. Here, it is
azservicebus
. - subscription_name: The name of the Service Bus subscription that the trigger listens to.
-
topic_name: The name of the Service Bus topic that the trigger listens to. In this example, it is
servicebustopicname
. -
connection: Refers to the application setting containing the connection string for the Azure Service Bus namespace. Here, it is
ServiceBusConnectionString
.
Other Triggers
- Cosmos DB Trigger: Responds to changes (inserts and updates) in an Azure Cosmos DB database by utilizing the change feed mechanism.
- Dapr Publish Output Binding: Allows functions to publish messages to a Dapr topic during execution, facilitating communication between microservices.
- Dapr Service Invocation Trigger: Enables functions to be invoked directly by other Dapr-enabled services, supporting service-to-service communication.
- Dapr Topic Trigger: Executes functions in response to messages published to a specific topic via Dapr's publish-subscribe messaging pattern.
- Event Grid Trigger: Activates functions when events are sent to an Azure Event Grid topic, allowing for reactive event-driven architectures.
Top comments (0)