How to Implement Azure Functions in any Language with Custom Handlers

How to Implement Azure Functions in any Language with Custom Handlers

Introduction of Custom Handlers with Demo using Go Language.

Introduction

Every Azure Function has its handler. Azure Functions supports handler implementations in many different languages but they may not provide a native implementation in your favorite language. That is where Custom Handlers comes at the scene.

The language of your choice must support HTTP communication because Custom Handler is a web server that receives events from Functions Host modified into HTTP requests with JSON payloads.

diagram.png

Functions Host is a bunch of JSON configuration files where you will define which trigger and bindings you want to use and of course the path to the executable file, which is our Custom Handler (Web Server).

Once the Trigger activates by event, the Functions Host sends a request payload to the Web Server. The payload can hold trigger and input binding data. The Web Server invokes the function and possibly returns a response payload. A response payload can be used for output binding.

Demo

The source code can be found on my GitHub.

Things you Need

Create Project

image.png

  • Click on the button highlighted in the image to create a new project.
  • You will be asked to choose basic settings. Select Custom language, HTTP Trigger, HttpExample as a name of the function, and Anonymous authorization level to define Function Host.

Create a Handler

  • Create a file in the root folder and save it as handler.go.
  • Copy/Paste this code into the file.
package main

import (
    "fmt"
    "log"
    "net/http"
    "os"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
    message := "This HTTP triggered function executed successfully. Pass a name in the query string for a personalized response.\n"
    name := r.URL.Query().Get("name")
    if name != "" {
        message = fmt.Sprintf("Hello, %s. This HTTP triggered function executed successfully.\n", name)
    }
    fmt.Fprint(w, message)
}

func main() {
    listenAddr := ":8080"
    if val, ok := os.LookupEnv("FUNCTIONS_CUSTOMHANDLER_PORT"); ok {
        listenAddr = ":" + val
    }
    http.HandleFunc("/api/HttpExample", helloHandler)
    log.Printf("About to listen on %s. Go to https://127.0.0.1%s/", listenAddr, listenAddr)
    log.Fatal(http.ListenAndServe(listenAddr, nil))
}

The function helloHandler is printing a simple message as an HTTP response. The form of the message depends on the value of the name parameter in the HTTP request.

If the value name is set, the response will be:

Hello, {name}. This HTTP triggered function was executed successfully.

If the value name is NOT set, the response will look like this:

This HTTP triggered function executed successfully. Pass a name in the query string for a personalized response.

  • Open a command line or PowerShell and compile Custom Handler into an executable file named handler.exe by using this command (installed Go):

go build handler.go

Modify host.json

Now you need to specify the file path for customHandler:defaulteExecutablePath property in host.json. If you created the *.exe file in the root folder, you can set the value to the handler. You also need to set the value of customHandler:eneableForwardingHttpRequest to true.

"customHandler": {
    "description": {
      "defaultExecutablePath": "handler",
      "workingDirectory": "",
      "arguments": []
    },
    "enableForwardingHttpRequest": true
  }

Run the function

  • Open a command line or PowerShell again and run this command (installed Azure Function Core Tools) in the root folder:

    func start

  • Navigate to the following URL to execute the function:

    localhost:7071/api/HttpExample?name=medium

image.png

If you get a similar response as me, you were successful.

  • You can also take a look into the console and see if your function was executed.

image.png

Summary

We found out what Custom Handlers of Azure Functions are, how they work, and how we can easily implement them with Go language. As a next step, I would recommend that you should try to publish the function into Azure.

Sources

itixo-logo-blue.png