Azure HTTP Triggers

An example integration where a Data Connector forwards events to an Azure HTTP Trigger.

Overview

This example uses a Data Connector to forward the events of all devices in a project to an Azure HTTP trigger. When receiving the HTTPS POST request, our trigger will verify both the origin and content of the request using a Signature Secret, then decode the data.

Prerequisites

The following points are assumed.

Azure Function

Depending on the runtime, parts of the HTTP Triggers creation process must be performed in a local environment. Ensure you have a working runtime environment for development using either the command line or Visual Studio Code.

Create a Function App

Function Apps combines several functions into a single unit for easier management and can be created in the Azure Portal by following this guide. Create a new Function App with the following configurations.

Python 3.8
Node.js 14
Python 3.8
  • Publish: Code.

  • Runtime Stack: Python.

  • Version: 3.8

Node.js 14
  • Publish: Code.

  • Runtime Stack: Node.js.

  • Version: 14 LTS

Edit Application Settings

We will verify the incoming POST request by a signature secret, as explained in the Data Connector Advanced Configurations. In your new Function App, add a secret to your App environment by adding a new Application Setting, as explained in this guide.

  • Name: DT_SIGNATURE_SECRET

  • Value: A unique password. We will use it later, so write it down.

Create an HTTP Trigger Function

Once the Function App is deployed, a new HTTP trigger function can be created. Azure has guides on creating and editing functions from either the command line or Visual Studio Code, so pick whichever you are most comfortable with.

By following one of the guides, create a new HTTP Trigger with the following configurations.

  • Authorization level: Anonymous.

Afterward, your project structure should end up looking something like this.

Python 3.8
Node.js 14
Python 3.8
project
├── host.json
├── yourNewFunction
│   ├── function.json
│   └── __init__.py
├── local.settings.json
└── requirements.txt
Node.js 14
project
├── host.json
├── yourNewFunction
│   ├── function.json
│   └── index.js
├── local.settings.json
└── package.json

Modify the HTTP Trigger

Once the function has been created, the desired behavior can be programmed. The following code snippet implements a simple HTTP trigger that listens for POST requests, then validates the content and origin using the Signature Secret and Checksum verification.

Python 3.8
Node.js 14
Python 3.8

In your HTTP Trigger function directory, edit the file __init__.py. The implementation is explained in detail on the Data Connector Receiving Events page.

__init__.py
import os
import hashlib
import azure.functions as func
import jwt # pip install pyjwt==2.0.1
# Fetch environment variables.
SIGNATURE_SECRET = os.environ.get('DT_SIGNATURE_SECRET')
def verify_request(body, token):
# Decode the token using signature secret.
try:
payload = jwt.decode(token, SIGNATURE_SECRET, algorithms=["HS256"])
except Exception as err:
print(err)
return False
# Verify the request body checksum.
m = hashlib.sha1()
m.update(body)
checksum = m.digest().hex()
if payload["checksum"] != checksum:
print('Checksum Mismatch')
return False
return True
def main(req: func.HttpRequest) -> func.HttpResponse:
# Extract necessary request information.
body = req.get_body()
token = req.headers['x-dt-signature']
# Validate request origin and content integrity.
if not verify_request(body, token):
return func.HttpResponse('Bad request', status_code=400)
#
# Further processing goes here.
#
return func.HttpResponse("OK", status_code=200)
Node.js 14

In your HTTP Trigger function directory, edit the file index.js. The implementation is explained in detail on the Data Connector Receiving Events page.

index.js
const crypto = require('crypto')
const jwt = require('jsonwebtoken') // npm install [email protected]
// Fetch environment variables.
const signatureSecret = process.env.DT_SIGNATURE_SECRET
function verifyRequest(body, token) {
// Decode the token using signature secret.
let decoded;
try {
decoded = jwt.verify(token, signatureSecret)
} catch(err) {
console.log(err)
return false
}
// Verify the request body checksum.
let shasum = crypto.createHash('sha1')
let checksum = shasum.update(JSON.stringify(body)).digest('hex')
if (checksum !== decoded.checksum) {
console.log('Checksum Mismatch')
return false
}
return true
}
module.exports = async function (context, req) {
// Extract necessary request information.
let body = req.body
let token = req.headers['x-dt-signature']
// Validate request origin and content integrity.
if (verifyRequest(body, token) === false) {
context.res = { status: 400, body: 'Bad request' }
return
}
//
// Further processing here.
//
context.res = { status: 200, body: 'OK' }
}

Dependency Management

Dependencies must be specified and included for your HTTP Trigger to run in the cloud.

Python 3.8
Node.js 14
Python 3.8

As explained here, edit requirements.txt to tell Azure which dependencies to install.

requirements.txt
pyjwt==2.0.1
Node.js 14

As explained here, in your project directory, install all requisite packages locally using npm install. This will generate a directory node_modules which is included when deploying.

npm install [email protected]

Deploy

Your function should now be ready for deployment. It is considered good practice to test- and experiment with your function locally, which you can read how-to here. The following command will deploy your function to the Function App created in the beginning.

func azure functionapp publish <APP_NAME>

Once the function is deployed, the Invoke URL is displayed in the output. Copy and save it as we will need it when creating our Data Connector.

...
Deployment successful.
Remote build succeeded!
Syncing triggers...
Functions in msdocs-azurefunctions-qs:
HttpExample - [httpTrigger]
THIS-> Invoke url: https://msdocs-azurefunctions-qs.azurewebsites.net/api/httpexample

Data Connector

To continuously forward the data to our newly created Cloud Function, a Data Connector with almost all default settings is sufficient. If you are unfamiliar with how Data Connectors can be created, refer to our Creating a Data Connector guide. The following configurations should be set.

  • Endpoint URL: The Invoke URL found in the previous step.

  • Signature Secret: The value of DT_SIGNATURE_SECRET environment variable set earlier.

Depending on your integration, it can also be smart to disable the event types you are not interested in. For instance, the NetworkStatusEvent is sent every Periodic Heartbeat and will by default be forwarded by the Data Connector if not explicitly unticked.

Test the Integration

If the integration was correctly implemented, the Success counter for your Data Connector should increment for each new event forwarded. This happens each Periodic Heartbeat or by touching a sensor to force a new event.

If instead the Error counter increments, a response containing a non-200 status code is returned.

  • Verify that the Data Connector endpoint URL is correct.

  • Azure provides a host of tools that can be used to monitor Azure Functions. Check the logs for any tracebacks that could explain why an error is returned.

Next steps

Your sensor data is now in the Azure environment, and you can start using it in their various services. Fortunately, Azure has some well-documented guides to get you started.

PostgreSQL Database

A database should be tailored to each specific use case. However, if you're uncertain, PostgreSQL (Postgres) is a good place to get started. The following guides will show you how to create a new Postgres database, then connect your HTTP Trigger to execute queries.