What are Function Triggers in Azure?

An Azure Function Trigger is a part of a function’s stack that tells Azure how the function is invoked. The most common function triggers are HTTP, Timer, and Blob:

  • The HTTP trigger tells Azure that the function will be invoked through an HTTP request. An example is a GET request used to obtain the most recent transactions in a banking app.
  • The Timer trigger tells Azure that the function should be executed at specific times. An example is a business app that generates sales reports by 6 pm daily.
  • Blob storage describes a function whose logic is executed when a file is uploaded or updated in an Azure Blob Storage instance.
widget

Trigger types

There are other trigger types aside from HTTP, Timer, and Blob:

Type Purpose
Timer Execute a function at a set interval.
HTTP Execute a function when an HTTP request is received.
Blob Execute a function when a file is uploaded or updated in Azure Blob storage.
Queue Execute a function when a message is added to an Azure Storage queue.
Azure Cosmos DB Execute a function when a document changes in a collection.
Event Hub Execute a function when an event hub receives a new event.

Timer triggers

Timer Triggers are defined using CRON syntax. Time Triggers CRON syntax puts the seconds first, then the minutes, hours, days, months, and days of the week.

widget

LIFX bulb example

The widget below shows a serverless timer function that sets the color of a LIFX bulb based on the outside temperature. The function fires every five minutes as indicated in the cron definition in the function.json file:

index.js
function.json
const axios = require('axios');
const LIFX = require('lifx-http-api');
let client = new LIFX({
bearerToken: process.env.LIFX_TOKEN
});
module.exports = function (context, myTimer) {
// build up the DarkSky endpoint
let endpoint = `${process.env.DS_API}/${process.env.DS_SECRET}/${
process.env.LAT
},${process.env.LNG}`;
// use axios to call DarkSky for weather
axios
.get(endpoint)
.then(data => {
let temp = Math.round(data.data.currently.temperature);
// make sure the temp isn't above 100 because that's as high as we can go
temp = temp < 100 ? temp : 100;
// determine the hue
let hue = 200 + (160 * (temp / 100));
// return Promise.all so we can resolve at the top level
return Promise.all([
data,
client.setState('all', { color: `hue:${hue}` })
]);
})
.then(result => {
// result[0] contains the darksky result
// result[1] contains the LIFX result
context.log(result[1]);
})
.catch(err => {
context.log(err.message);
});
};

Uses of time triggers

  1. Generating reports
  2. Cleaning a database
  3. Sending notifications

HTTP triggers

HTTP triggers execute function logic when they receive a HTTP request. Examples of HTTP requests are:

  1. GET request for requesting data
  2. POST request for creating resources
  3. PUT request for modifying resources
  4. DELETE requests for deleting resources

Several HTTP verb handlers can be contained in a function. This means that an entire application can be composed of serverless functions.

A sample HTTP trigger function is given below. It is similar to the typical express routing handler function, however, the serverless function below uses an HTTP post request to submit a link to a given subreddit:

index.js
function.json
submit-link.js
const axios = require('axios')
const URL = 'https://oauth.reddit.com/api/submit'
const submitLink = async data => {
/*
sr: name of a subreddit
title: title of the submission. up to 300 characters long
url: a valid URL
api_type: the string json
kind: one of (link, self, image, video, videogif)
resubmit: boolean
*/
const link = {
title: data.title,
sr: data.sr,
url: data.url,
api_type: 'json',
kind: 'link',
}
const response = await axios({
url: URL,
method: 'post',
headers: {
Authorization: `bearer ${process.env.REDDIT_KEY}`,
'user-agent': 'node.js',
},
params: link,
})
return response.data
}
module.exports = submitLink

HTTP triggers can be used for building:

  1. Webforms
  2. Mobile applications
  3. Aggregators

Blob triggers

This trigger executes a function when a file is uploaded or updated in Azure Blob storage. This function type can be used to:

  1. Notify an admin that a file has been uploaded
  2. Convert a newly uploaded image file to a thumbnail
  3. Update a database with an image’s metadata

Other trigger types

Several other trigger types exist as Azure has trigger types for most use cases. This versatility allows developers to create a wide variety of applications using Azure Functions.