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:
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 are defined using CRON syntax. Time Triggers CRON syntax puts the seconds first, then the minutes, hours, days, months, and days of the week.
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:
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); }); };
HTTP triggers execute function logic when they receive a HTTP request. Examples of HTTP requests are:
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:
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:
This trigger executes a function when a file is uploaded or updated in Azure Blob storage. This function type can be used to:
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.
RELATED TAGS
CONTRIBUTOR
View all Courses