A Quick Guide to Creating Azure Function with Golang
AI generated image via Medium

A Quick Guide to Creating Azure Function with Golang

Azure Functions, a serverless computing service by Microsoft Azure, supports various programming languages like C#, JavaScript, Python, and Java. It seamlessly integrates with Azure services, offering event-driven functions with automatic scaling based on demand. This guide simplifies the process of deploying your Golang backend service to Azure Function as a custom runtime.

Prerequisites

  • Basic knowledge of creating an HTTP server using Golang.
  • Azure setup on your desktop.

Installation

  1. Download and install Go
  2. Install the Azure CLI
  3. Gin Web Framework Quickstart

Step 1: Create a Golang Server

Create a server.go file and add the following code:

package main
import (
    "net/http"
    "os"
    "github.com/gin-gonic/gin"
)

func get_port() string {
    port := ":8080"
    if val, ok := os.LookupEnv("FUNCTIONS_CUSTOMHANDLER_PORT"); ok {
        port = ":" + val
    }
    return port
}

func main() {
    router := gin.Default()
    router.GET("/api/hello", func(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{
            "message": "Hello, World!",
        })
    })
    port := get_port()
    router.Run(port)
}

Note: Ensure the get_port function is present. The FUNCTIONS_CUSTOMHANDLER_PORT environment variable is essential for Azure Functions to function properly. I lost many hours trying to figure this out as just like any other developer I do not enjoy reading docs even though I should be.

Azure Functions automatically assigns a port value to FUNCTIONS_CUSTOMHANDLER_PORT when a custom handler is used. The platform dynamically assigns a port to ensure proper communication between the Azure Functions runtime and the custom handler, simplifying the configuration process for developers.

Step 2: Set Up Golang Dependencies

go mod init <service-name>
go mod tidy
# To run it locally
go run server.go

Step 3: Configure Azure Function Files

func init --worker-runtime custom
func new -l Custom -t HttpTrigger -n hello -a anonymous
  • func init --worker-runtime custom: Initializes a new Azure Functions project with a custom worker runtime. It creates a host.json file for configuring the Azure Functions host.
  • func new -l Custom -t HttpTrigger: Creates a new function with a custom language worker and an HTTP trigger. This command generates necessary files, including function.json that defines the function’s trigger and bindings.

Modify host.json:

Add this field inside customHandler:

"enableForwardingHttpRequest": true

Update the value of defaultExecutablePath to your build file path:

"defaultExecutablePath": "server"

Modify hello/function.json:

Add this field below methods inside bindings:

"route": "hello"

We are using hello as the value since our endpoint is /hello.

Step 4: Run Locally with Azure CLI

func start --verbose

Step 5: Create Function in Azure Console

Go to Azure Console and create a function with the specified settings.

Azure Function Creation

Step 6: Deployment

In your terminal, build your project:

GOOS=linux GOARCH=amd64 go build server.go

Publish your function to Azure:

func azure functionapp publish <your-function-name>

Tip: Make sure to use GOOS and GOARCH values while building, especially if you are on macOS, to avoid platform mismatch issues after deployment in the Linux environment of Azure Functions.

Azure Function Deployment

Upon successful deployment, you will receive a confirmation message in your terminal and see the changes reflected inside the Azure console for the function. Congratulations, your Golang backend service is now deployed on Azure Functions!

You can take a look at the source file at Github.


Full size image