Creating an Event Function Using a Container Image Built with Go

For general details about how to use a container image to create and execute an event function, see Creating an Event Function Using a Container Image and executing the Function.

This chapter introduces how to create an image using the Go language and perform local verification for event functions.

Note

You need to implement an HTTP server in the image listening to port 8000 to receive requests.

Following request path is required:

  • POST /invoke is the function execution entry where trigger events are processed.

To initialize the function configuration, implement the “init()” function, which Go will execute automatically.

Note

FunctionGraph currently does not support initializer functions for event functions.

Complete code can be found on Github: Container Event Sample

Step 1: Create the Project

In this example we use the gin framework to create an HTTP server.

For details about gin framework, see:

Create Go Module

1. Create directories for your project and navigate to it

# cretate project directory and src subdirectory
mkdir -p container-event/src
# navigate to project directory
cd container-event

2. Initialize a Go module

Run the following command to initialize a new Go module:

go mod init container-event

3. Add dependencies

Run the following commands to add the necessary dependencies:

# add gin framework
go get -u github.com/gin-gonic/gin

# add otc-functiongraph-go-runtime package for use with FunctionGraph events
go get -u github.com/opentelekomcloud-community/otc-functiongraph-go-runtime

4. Resulting go.mod

The resulting go.mod file should look like this:

module container-event-sample

go 1.24.0

require github.com/gin-gonic/gin v1.11.0

require (
  github.com/bytedance/gopkg v0.1.3 // indirect
  github.com/bytedance/sonic v1.15.0 // indirect
  github.com/bytedance/sonic/loader v0.5.0 // indirect
  github.com/cloudwego/base64x v0.1.6 // indirect
  github.com/gabriel-vasile/mimetype v1.4.12 // indirect
  github.com/gin-contrib/sse v1.1.0 // indirect
  github.com/go-playground/locales v0.14.1 // indirect
  github.com/go-playground/universal-translator v0.18.1 // indirect
  github.com/go-playground/validator/v10 v10.30.1 // indirect
  github.com/goccy/go-json v0.10.5 // indirect
  github.com/goccy/go-yaml v1.19.2 // indirect
  github.com/json-iterator/go v1.1.12 // indirect
  github.com/klauspost/cpuid/v2 v2.3.0 // indirect
  github.com/leodido/go-urn v1.4.0 // indirect
  github.com/mattn/go-isatty v0.0.20 // indirect
  github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
  github.com/modern-go/reflect2 v1.0.2 // indirect
  github.com/pelletier/go-toml/v2 v2.2.4 // indirect
  github.com/quic-go/qpack v0.6.0 // indirect
  github.com/quic-go/quic-go v0.59.0 // indirect
  github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
  github.com/ugorji/go/codec v1.3.1 // indirect
  go.uber.org/mock v0.6.0 // indirect
  golang.org/x/arch v0.23.0 // indirect
  golang.org/x/crypto v0.47.0 // indirect
  golang.org/x/net v0.49.0 // indirect
  golang.org/x/sys v0.40.0 // indirect
  golang.org/x/text v0.33.0 // indirect
  google.golang.org/protobuf v1.36.11 // indirect
)

Implement the function

Change folder to src folder:

# navigate to src directory
cd src

The FunctionGraph program implements an HTTP server to process POST invoke requests and give a response.

  • Create a eventhandler.go file,

  • import the gin dependency package,

  • implement a function named init() to initialize the configuration.

    Go runs this function named init automatically before any other part of the package.

  • implement a function handler (method POST and path /invoke).

Following example code shows the implementation of HTTP Server in file eventhandler.go:

src/eventhandler.go
package main

import (
  "bytes"
  "fmt"
  "io"
  "time"

  "github.com/gin-gonic/gin" // Import the Gin framework.
)

// go init function is called before main function.
func init() {
  fmt.Println("init in main.go ")
}

// Logger is a middleware function used
// to record request and response information.
func Logger() gin.HandlerFunc {

  return func(c *gin.Context) {
    logTimeFormat := "2006-01-02T15:04:05.000Z"
    start := time.Now()

    // Get the request ID from the request header.
    requestID := c.GetHeader("X-Cff-Request-Id")
    reqBody, _ := c.GetRawData()

    fmt.Printf("%s [INFO] Request:  %s %s %s %s\n", time.Now().UTC().Format(logTimeFormat), requestID, c.Request.Method,
      c.Request.RequestURI, reqBody)

    // Assign Back the request body as body can only be read once
    // by default in Gin framework.
    c.Request.Body = io.NopCloser(bytes.NewBuffer(reqBody))

    c.Next()

    end := time.Now()
    latency := end.Sub(start)
    respBody := string(rune(c.Writer.Size()))
    fmt.Printf("%s [INFO] Response: %s %s %s %s (%v)\n", time.Now().UTC().Format(logTimeFormat), requestID, c.Request.Method,
      c.Request.RequestURI, respBody, latency)
  }
}

// main is the entry point of the application.
// In Production set Environment variable
// GIN_MODE=release
func main() {

  // Create a Gin router.
  router := gin.New()

  // Global middleware
  router.Use(Logger())

  // Recovery middleware recovers from any panics and writes
  // a 500 if there was one.
  router.Use(gin.Recovery())

  // Register a route (/invoke) that processes HTTP POST requests.
  // When FunctionGraph sends a POST request to /invoke, the Gin framework
  // calls the invokeSampleData function to process the request.
  router.POST("/invoke", invokeSampleData)

  // Handle undefined routes
  router.NoRoute(func(c *gin.Context) {
    c.JSON(404, gin.H{"code": "PAGE_NOT_FOUND" + c.Request.Method + " " + c.Request.RequestURI, "message": "Page not found"})
  })

  // Start the HTTP server and listen to port 8000.
  err := router.Run(":8000")
  if err != nil {
    return
  }
}

The logic to process the event is implemented in the function invokeSampleData in invokeSampleData.go.

src/invokeSampleData.go
package main

import (
  "encoding/json"
  "fmt"
  "net/http"

  "github.com/gin-gonic/gin" // Import the Gin framework.
)

// SampleData represents the structure of the expected JSON payload:
//
//  { "key": "value"}
//
// in the request body.
type SampleData struct {
  Key string `json:"key"`
}

// invokeSampleData is a function that processes
// POST requests sent to the /invoke route.
func invokeSampleData(c *gin.Context) {
  // Read the request body.
  reqBody, _ := c.GetRawData()

  fmt.Printf("Received request body: %s\n", string(reqBody))

  // Print all request headers.
  // for name, values := range c.Request.Header {
  //  // Loop over all values for the name.
  //  for _, value := range values {
  //    fmt.Printf(" %s: %s\n", name, value)
  //  }
  // }

  var sampleData SampleData

  err := json.Unmarshal(reqBody, &sampleData)
  if err != nil {
    fmt.Println("Unmarshal failed")
    c.String(http.StatusBadRequest, "invalid data")
    return
  }

  fmt.Printf("Key: %s\n", sampleData.Key)

  c.String(http.StatusOK, "Received key: %s", sampleData.Key)

}

Run and Test server from code

To test server from source code, execute in the container-event/src folder:

go run .

The server starts listening on port 8000.

You can use curl to send a test request to the function using a new shell.

curl -X POST -H 'Content-Type: application/json' localhost:8000/invoke -d '{"key":"Hello World of FunctionGraph"}'

The expected response is:

Received key: Hello World of FunctionGraph

Create a Makefile

To simplify the development and testing process, create a Makefile in the container-event folder:

Makefile
TARGET_PATH=target
#DOCKER_FILE=Dockerfile
DOCKER_FILE=Dockerfile.alpine

IMAGE_NAME=custom_container_event_example

build: clean
  mkdir -p $(TARGET_PATH)
  GOARCH=amd64 GOOS=linux CGO_ENABLED=0 && \
  cd src && \
  go build -o ../$(TARGET_PATH)/eventhandler .

run_local:
  cd $(TARGET_PATH) && ./eventhandler

docker_build: build
  docker buildx build \
    --platform linux/amd64 \
    --build-arg FILE_PATH=$(TARGET_PATH) \
    --file $(DOCKER_FILE) \
    --tag $(IMAGE_NAME):latest .

docker_run_local:
  docker container run \
    --rm \
    --platform linux/amd64 \
    --publish 8000:8000 \
    --name container_event_example \
    $(IMAGE_NAME):latest

docker_push: docker_build
  # see: https://docs.otc.t-systems.com/software-repository-container/umn/image_management/obtaining_a_long-term_valid_login_command.html#swr-01-1000
  docker login -u $(OTC_SDK_PROJECTNAME)@$(OTC_SDK_AK) -p $(OTC_SWR_LOGIN_KEY) $(OTC_SWR_ENDPOINT)
  docker tag $(IMAGE_NAME):latest $(OTC_SWR_ENDPOINT)/$(OTC_SWR_ORGANIZATION)/$(IMAGE_NAME):latest
  docker push $(OTC_SWR_ENDPOINT)/$(OTC_SWR_ORGANIZATION)/$(IMAGE_NAME):latest

docker_all: build docker_build docker_push

test_local:
  # execute a curl 
  curl -X POST -H 'Content-Type: application/json' -d '{"key":"Hello World of FunctionGraph"}' localhost:8000/invoke

clean:
  rm -rf $(TARGET_PATH)

all: build docker_build

.PHONY: build run_local docker_build docker_run_local docker_push docker_all test_local clean

Build the program

Build the program either using go build or the Makefile target build:

Run the following command in the container-event folder to build the program:

# create output folder (if not already existing)
mkdir -p target

# build the program
GOARCH=amd64 GOOS=linux CGO_ENABLED=0 && cd src && go build -o ../target/eventhandler .

Run and Test program locally

1. Run the program locally

Run the program locally either using executable or the Makefile target run_local:

Run the following command in the container-event folder to run the compiled program:

./target/eventhandler

In the terminal, you should see output similar to the following:

init in main.go
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env:   export GIN_MODE=release
- using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] POST   /invoke                   --> main.invoke (4 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://github.com/gin-gonic/gin/blob/master/docs/doc.md#dont-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :8000

2. Test the program locally

Test the program locally either using curl or the Makefile target test_local:

Run the following command in a new terminal to test the program using a curl command:

curl -X POST -H 'Content-Type: application/json' -d '{"key":"Hello World of FunctionGraph"}' localhost:8000/invoke

You should see output similar to the following:

Received key: Hello World of FunctionGraph

Step 2: Build the Container Image

Create a Dockerfile

Create a Dockerfile in the container-event folder to define the image.

Note

  • In the cloud environment, UID 1003 and GID 1003 are used to start the container by default.
    The two IDs can be modified by choosing Configuration > Basic Settings > Container Image Override
    on the function details page. They cannot be root or a reserved ID.
  • If the base image of the Alpine version is used, run the addgroup and adduser instead of groupadd and useradd commands.
  • You an use any base image that meets your application requirements.

Following are two example Dockerfiles, one using Ubuntu base image and another using Alpine base image.

Note

  • Ubuntu images are larger in size but come with more pre-installed libraries.

  • Alpine images are smaller in size but may require additional libraries depending on the application requirements.

Example Dockerfile using Ubuntu base image:

Dockerfile Ubuntu
# Example Dockerfile for OTC FunctionGraph Container Event Function 
# using Ubuntu base image
FROM amd64/ubuntu:22.04

# set environment variables
ENV HOME=/home/paas

ENV GROUP_ID=1003
ENV GROUP_NAME=paas_user

ENV USER_ID=1003
ENV USER_NAME=paas_user

# create non root user and home directory
RUN mkdir -m 550 ${HOME} && \
   # add group and user with specific IDs
   groupadd -g ${GROUP_ID} ${GROUP_NAME} && \
   # add user with specific UID and GID
   useradd -u ${USER_ID} -g ${GROUP_ID} ${USER_NAME}

# copy content of target folder
ADD ./target ${HOME}

# adjust permissions
RUN chown -R ${USER_ID}:${GROUP_ID} ${HOME} && \
    chmod -R 775 ${HOME}

# switch to non root user
USER ${USER_NAME}

# switch to working directory
WORKDIR ${HOME}

# expose port and entrypoint
# FunctionGraph invokes container event function through port 8000
EXPOSE 8000

ENTRYPOINT ["/home/paas/eventhandler"]

Build and verify the image locally

1. Build the image

Build the image either using docker build or the Makefile target docker_build:

Run the following command in the container-event folder to build the image:

docker buildx build \
   --platform linux/amd64 \
   --build-arg FILE_PATH=target \
   --file Dockerfile \
   --tag custom_container_event_example:latest .

Note

Replace Dockerfile with Dockerfile.alpine in the above command to build the image using the Alpine base image.

2. Run the image locally

Run the image either using docker run or the Makefile target docker_run_local:

Run the following command in the container-event folder to run the image:

docker container run --rm \
  --platform linux/amd64 \
  --publish 8000:8000 \
  --name container_event_example \
  custom_container_event_example:latest

3. Test the image locally

Test the image either using curl or the Makefile target test_local:

Run the following command in a new terminal to test the image using a curl command:

curl -X POST -H 'Content-Type: application/json' -d '{"key":"Hello World of FunctionGraph"}' localhost:8000/invoke

You should see output similar to the following:

*** Received key: Hello World of FunctionGraph ***

Step 3: Upload the Container Image to SWR (SoftWare Repository for Container)

For details on SWR (SoftWare Repository for Container), see:

Prerequisites

Upload the image to SWR

To upload the container image to SWR, following values are needed:

Parameter

Description

OTC_SDK_PROJECTNAME

Your project name.
To obtain this, see: Obtaining a Project ID in API usage guide but use the project name instead of the project ID.

OTC_SDK_AK

Your Access Key

OTC_SWR_LOGIN_KEY

The login key for SWR.
For details see: Obtaining a Long-Term Docker Login Command in the Software Repository for Container user manual.

It can be generated using the access key ${OTC_SDK_AK} and secret key ${OTC_SDK_SK} as follows:
export OTC_SWR_LOGIN_KEY=$(printf "${OTC_SDK_AK}" | \
        openssl dgst -binary -sha256 -hmac "${OTC_SDK_SK}" | \
        od -An -vtx1 | sed 's/[ \n]//g' | sed 'N;s/\n//')

OTC_SWR_ENDPOINT

SWR endpoint, e.g. swr.eu-de.otc.t-systems.com

OTC_SWR_ORGANIZATION

Your SWR organization name

IMAGE_NAME

The name of your container image

Set the environment variables:
export OTC_SDK_PROJECTNAME=<your_project_name>
export OTC_SDK_AK=<your_access_key>
export OTC_SDK_SK=<your_secret_key>
export OTC_SWR_LOGIN_KEY=$(printf "${OTC_SDK_AK}" | \
        openssl dgst -binary -sha256 -hmac "${OTC_SDK_SK}" | \
        od -An -vtx1 | sed 's/[ \n]//g' | sed 'N;s/\n//')
export OTC_SWR_ENDPOINT=swr.eu-de.otc.t-systems.com
export OTC_SWR_ORGANIZATION=<your_swr_organization>
export IMAGE_NAME=custom_container_event_example

Upload the image to SWR either using shell commands or the Makefile target docker_push:

Run the following commands in the container-event folder to upload the image to SWR:

1. Login to SWR
  docker login -u $(OTC_SDK_PROJECTNAME)@$(OTC_SDK_AK) -p $(OTC_SWR_LOGIN_KEY) ${OTC_SWR_ENDPOINT}
2. Tag the image
  docker tag $(IMAGE_NAME):latest ${OTC_SWR_ENDPOINT}/$(OTC_SWR_ORGANIZATION)/$(IMAGE_NAME):latest
3. Push the image to SWR
  docker push ${OTC_SWR_ENDPOINT}/$(OTC_SWR_ORGANIZATION)/$(IMAGE_NAME):latest

Step 4: Create an Event Function Using the Container Image

  1. In the left navigation pane of the management console, choose Compute > FunctionGraph. On the FunctionGraph console, choose Functions > Function List from the navigation pane.

  2. Click Create Function in the upper right corner. On the displayed page, select Container Image for creation mode.

  3. Set the basic function information.

    • Function Type: Select Event Function.

    • Region: The default value is used. You can select other regions.

      Regions are geographic areas isolated from each other. Resources are region-specific and cannot be used across regions through internal network connections. For low network latency and quick resource access, select the nearest region.

    • Function Name: Enter e.g. custom_container_event.

    • Enterprise Project: The default value is default. You can select the created enterprise project.

      Enterprise projects let you manage cloud resources and users by project.

    • Agency: Select an agency with the SWR Admin permission. If no agency is available, create one by referring to Creating an Agency.

    • Container Image: Enter the image uploaded to SWR. The format is: {SWR_endpoint}/{organization_name}/{image_name}:{tag}.

      Example: swr.eu-de.otc.t-systems.com/my_organization/custom_container_event_example:latest.

  4. Advanced Settings: Collect Logs is disabled by default. If it is enabled, function execution logs will be reported to Log Tank Service (LTS). You will be billed for log management on a pay-per-use basis.

    Parameter

    Description

    Log Configuration

    You can select Auto or Custom.

    • Auto: Use the default log group and log stream. Log groups prefixed with “functiongraph.log.group” are filtered out.

    • Custom: Select a custom log group and log stream. Log streams that are in the same enterprise project as your function.

    Log Tag

    You can use these tags to filter function logs in LTS.
    You can add 10 more tags.
    Tag key/value: Enter a maximum of 64 characters.
    Only digits, letters, underscores (_), and hyphens (-) are allowed.
  5. After the configuration is complete, click Create Function.

See also: Step 4: Creating Function in the user manual.

Step 5: Test the Event Function

On the function details page, click Test. In the displayed dialog box, create a test event:

  • Select blank-template,

  • set Event Name to helloworld,

  • modify the test event as follows,

    {
        "key": "Hello World of FunctionGraph"
    }
    
  • and click Create.

See also: Step 5: Testing the Function in the user manual.

Step 6: View the Execution Result

Click Test and view the execution result on the right.

You should see output similar to the following:

Execution Result

The execution result contains the following sections:

  • The Function Output section displays the function’s return value.

  • The Log Output section displays the logs generated during function execution.

    Note

    This page displays a maximum of 2K logs.

  • The Summary section displays key information from the Log.

Deploy the Event Function using Terraform

For details on how to deploy using Terraform, see Deploying an Event Function container Image using Terraform.