This topic describes the structural characteristics, sample code, and FAQ of event handlers in a custom container.

Background information

For a custom container function in web server mode, Function Compute forwards the common headers, request body, POST method, /invoke, and /initialize to the HTTP server in the container. You can use the function signatures, such as context and event, of officially supported runtimes, such as the Golang runtime. You can also use the request headers and body as input parameters to define the service logic of a function. For more information, see Event handlers.

For a custom container function in non-web server mode, Function Compute adds the body to the startup environment of the container by using environment variables. You can use os.GetEnv("FC_CUSTOM_CONTAINER_EVENT") to obtain event information and perform custom operations.

Function invocation

If a function in web server mode uses an event handler, the HTTP server needs to only implement the logic that corresponds to the /invoke path and the POST method.

Path Request Expected response
POST /invoke
  • Request body: the function input, which is the payload that you specify when you call the InvokeFunction operation.
  • Request header: common request headers. For more information, see Common request headers in Function Compute.
    Important Content-Type must be application/octet-stream.
Response body: the return value of the function handler. The return value includes the response code and response header.
  • HTTP status code (StatusCode)
    • 200: succeeded
    • 404: failed
  • Response header (x-fc-status)
    • 200: succeeded
    • 404: failed
You can include the x-fc-status field in response headers to report to Function Compute whether the local function is successfully invoked.
  • If you do not specify a value for the x-fc-status field, Function Compute considers the invocation successful. If an error occurs during function execution, the system does not report the error to Function Compute. In this case, the business logic may not be affected, but the observability of Function Compute is affected. The following figure provides an example.image8hanshujisuanruntime
  • If you specify a value for the x-fc-status field, the system reports function invocation failures to Function Compute by using the x-fc-status field. If an error occurs during function invocation, the system records the error stack information in logs. The following figure provides an example.image9runtimefc
Note We recommend that you specify the StatusCode and x-fc-status fields in the HTTP response.

Sample code for a custom container in web server mode

In the following Node.js Express example, the POST method and /initialize are called by Function Compute when a function instance is initialized. The POST method and /invoke serve as handlers when Function Compute is called. The context and event parameters are obtained from req.headers and req.body, and the results of the function invocation are then returned as an HTTP response.

'use strict';

const express = require('express');

// The constants.
const PORT = 9000;
const HOST = '0.0.0.0';
const app = express();
// Parse the JSON-formatted request body.
app.use(express.json())


// The example of the Initializer hook. You must configure the initializer hook when you create the function.
app.post('/initialize', (req, res) => {
  console.log(req.body)
  res.send('Hello FunctionCompute, /initialize\n');
});


// Invoke the event function.
app.post('/invoke', (req, res) => {
  console.log(req.body)
  res.send('Hello FunctionCompute, event function\n');
});

var server = app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

server.timeout = 0; // The timeout period for the server. Set this parameter to 0. The value 0 indicates that the server never times out.
server.keepAliveTimeout = 0; // keepalive, never timeout

Sample code for a custom container function in non-web server mode

In the following Python image example, the event-triggering content is obtained by using FC_CUSTOM_CONTAINER_EVENT.

import os

if __name__ == '__main__':
    FC_EVENT = os.environ['FC_CUSTOM_CONTAINER_EVENT']
    print("Hello serverless image")
    print("FC event is: " + FC_EVENT)

Examples

Python

C++

Node.js

FAQ