Azure Functions Custom Handlers

Baller addition (in preview) to Azure Functions:

Azure Functions offers first-class support for a limited number of languages. Now in preview, register custom handlers by providing a lightweight HTTP server in any desired language. Use this new capability to extend the language support for your applications, enabling the use of languages or language versions not provided by Azure Functions.

Upon receiving a request the Functions Host will proxy the call to Custom Handler’s Web Server, which can run any language. Its response will then be used as an output binding payload to the target.

Configuration of the Custom Handler happens using a host.json in the root to tell which process to run:

{
    "version": "2.0",
    "httpWorker": {
        "description": {
            "defaultExecutablePath": "node",
            "defaultWorkerPath": "server.js"
        }
    }
}

The contents of your function goes into its own directory. Your code is accompanied by a function.json in which you define the bindings that your function uses. You can bind to queueTrigger for example and output to a queue. HTTP of course is also supported, as per this example:

{
  "bindings": [
    {
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": ["get", "post"]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

It takes in the Request req and expects a Response res as output.

const express = require("express");
const app = express();

app.use(express.json());

const PORT = process.env.FUNCTIONS_HTTPWORKER_PORT;

const server = app.listen(PORT, "localhost", () => {
  console.log("Your port is ${PORT}");
  const { address: host, port } = server.address();
  console.log("Example app listening at http://${host}:${port}");
});

app.get("/hello", (req, res) => {
  res.json("Hello World!");
});

app.post("/hello", (req, res) => {
  res.json({ value: req.body });
});

What I’m currently missing in the documentation and examples repository though is an example using a Dockerfile to say, for example, run PHP code. On Twitter I found this example by Anthony Chu (PM Azure Functions):

The code itself is not available (as a repo) yet, but by looking at the screenshot you can get an idea of how it all comes together. Will do a follow-up post once example PHP code becomes available 😉

Azure Functions custom handlers →
Sample code for Azure Functions custom handlers (GitHub) →

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.