This topic describes the input parameters and responses of event functions for custom container runtime and provides sample code.

Background

In a custom container runtime, Function Compute forwards the common headers, request body, POST method, and /invoke and /initialize paths to the HTTP server in the container. You can use function signatures similar to context and event of officially supported runtimes such as 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 function calls in a custom runtime.

Input parameters of functions

  • event: the body of the POST request
  • context:
    • The x-fc-access-key-id, x-fc-access-key-secret, and x-fc-security-token headers are used to obtain temporary credentials for the service role to access other Alibaba Cloud services.
    • The x-fc-request-id header is used to obtain the current request ID.
    • For more information about request headers, see Common headers.

Function response structure

The results of a function call are returned as an HTTP response.

Sample code

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 path 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 call are then returned as an HTTP response.

'use strict';

const express = require('express');

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// HTTP function invocation
const app = express();
app.get('/*', (req, res) => {
  res.send('Hello FunctionCompute, http function\n');
});

// Event function invocation
app.post('/invoke', (req, res) => {
  res.send('Hello FunctionCompute, event function\n');
});

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

server.timeout = 0; // never timeout
server.keepAliveTimeout = 0; // keepalive, never timeout