This topic describes the structural features, instance usage, and FAQ of event handlers in a custom runtime.

Background information

In a custom runtime, Function Compute forwards the common headers, the request body, the POST method, the /invoke path, and the /initialize path to your HTTP server. You can build an input parameter similar to the context parameter in an official runtime based on the information in common headers. You can also build an input parameter similar to the event parameter in an official runtime based on the request body of the invoked function. fccustomeruntimeruntime

Function invocation

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

PathRequestExpected 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.

Examples

You can use Serverless Devs to migrate your applications to Function Compute with a few clicks. The following example shows how to use Serverless Devs to deploy and invoke a function in an efficient manner. You can modify the sample code based on your business requirements.

PHP

Go

Node.js

Python

Ruby

C++

TypeScript

Powershell

Lua

Dart

Rust

FAQ