Code development for Custom Container runtimes

更新时间:
复制 MD 格式

Custom Container runtimes host your HTTP server inside a container. Function Compute converts every function invocation into an HTTP request, forwards it to your server, and converts the HTTP response back into a function invocation response.

How it works

image

Function Compute supports two invocation methods:

  • HTTP calls (recommended): Invoke a function via an HTTP trigger or a custom domain name. Function Compute passes HTTP requests and responses between the client and your HTTP server with minimal transformation.

  • API calls: Invoke a function by calling the InvokeFunction operation — for example, through an SDK or an event source. Function Compute converts the InvokeFunction request into an HTTP POST request and forwards it to the fixed path /invoke on your HTTP server.

The request and response format your HTTP server receives depends on the invocation method.

Limitations

HTTP trigger

Only one HTTP trigger can be created for an HTTP function in each version or alias. For more information, see Manage versions and Manage aliases.

Request limits

Request headers cannot include custom fields starting with x-fc-, or the following reserved fields:

  • connection

  • keep-alive

The system returns status code 400 and error code InvalidArgument if a request exceeds any of the following limits:

ResourceLimit
Headers size8 KB (total of all keys and values)
Path size4 KB (including all query parameters)
Body size — synchronous invocation32 MB
Body size — asynchronous invocation128 KB

Response limits

Response headers cannot include custom fields starting with x-fc-, or the following reserved fields:

  • connection

  • content-length

  • date

  • keep-alive

  • server

  • content-disposition:attachment

When using the default aliyuncs.com domain, Function Compute forcibly adds content-disposition: attachment to all response headers, causing browsers to download responses as attachments instead of rendering them. To remove this restriction, configure a custom domain name. For more information, see Configure a custom domain name.

The system returns status code 502 and error code BadResponse if a response exceeds the following limit:

ResourceLimit
Headers size8 KB (total of all keys and values)

Path routing

Bind a custom domain name to map different HTTP paths to functions. For more information, see Configure a custom domain name.

HTTP calls (recommended)

Function Compute passes HTTP requests from clients to your HTTP server and passes HTTP responses back to clients in passthrough mode. Some system-reserved fields are not passed. For details, see Limitations.

Request headers

When invoking a function via an HTTP trigger or a custom domain name, use the following request headers to control Function Compute behavior:

ParameterTypeRequiredExampleDescription
X-Fc-Invocation-TypeStringNoSyncInvocation mode. Valid values: Sync (synchronous invocation) and Async (asynchronous invocation). For more information, see Invocation methods.
X-Fc-Log-TypeStringNoTailLog return method. Valid values: Tail (returns the last 4 KB of logs for the current request) and None (returns no logs; default).

Response headers

Function Compute automatically adds the following response header to HTTP call responses:

ParameterDescriptionExample
X-Fc-Request-IdRequest ID of the function invocation.dab25e58-9356-4e3f-97d6-f044c4****

API calls

When you invoke a function using the InvokeFunction operation, Function Compute converts the request into an HTTP request and forwards it to your HTTP server at the fixed path /invoke.

Request conversion (InvokeFunction to HTTP)

InvokeFunction fieldHTTP request
event parameterRequest body
Path/invoke (fixed)
MethodPOST (fixed)
Content-Typeapplication/octet-stream (fixed)

Response conversion (HTTP to InvokeFunction)

HTTP response fieldInvokeFunction response
Response bodyResponse body
Response headersLost during conversion
Status codeLost during conversion

Conversion examples

Request:

InvokeFunction requestHTTP request received by the HTTP server
"hello world"POST /invoke HTTP/1.1<br>Host: 21.0.X.X<br>Content-Length: 11<br>Content-Type: application/octet-stream<br><br>hello world

Response:

HTTP responseInvokeFunction response
HTTP/1.1 200 OK<br>Date: Mon, 10 Jul 2025 10:37:15 GMT<br>Content-Type: application/octet-stream<br>Content-Length: 11<br>Connection: keep-alive<br><br>hello worldhello world
HTTP/1.1 400 Bad Request<br>Date: Mon, 10 Jul 2023 10:37:15 GMT<br>Content-Type: application/octet-stream<br>Content-Length: 28<br>Connection: keep-alive<br><br>{"errorMessage":"exception"}{"errorMessage":"exception"}

Sample code

The following Python (Flask) example implements an HTTP server that handles both HTTP calls and API calls. The server listens on port 9000 and handles GET, POST, PUT, and DELETE requests on any path.

import os
from flask import Flask, request

REQUEST_ID_HEADER = 'x-fc-request-id'
app = Flask(__name__)


@app.route('/', defaults={'path': ''})
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def hello_world(path):
    # Read the request ID injected by Function Compute
    rid = request.headers.get(REQUEST_ID_HEADER)
    # Read the request body
    data = request.stream.read()
    print("Path: " + path)
    print("Data: " + str(data))
    # Return a response body, status code, and a custom header
    return "Hello, World!", 200, [('Function-Name', os.getenv('FC_FUNCTION_NAME'))]


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=9000)

Key points:

  • @app.route('/', defaults={'path': ''}) — handles requests to the root path.

  • @app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE']) — handles requests to any other path and passes the path value to hello_world as the path parameter.

  • request.headers.get(REQUEST_ID_HEADER) — reads the x-fc-request-id header injected by Function Compute into every request.

  • request.stream.read() — reads the request body.

  • return "Hello, World!", 200, [('Function-Name', os.getenv('FC_FUNCTION_NAME'))] — returns a response body of "Hello, World!", status code 200, and a Function-Name response header sourced from the FC_FUNCTION_NAME environment variable.

What's next