All Products
Document Center

Function Compute:Lifecycle hooks for function instances

Last Updated:Feb 02, 2024

This topic describes how to implement instance lifecycle hooks in a Custom Container runtime.

Lifecycle hooks

After you implement and configure lifecycle hooks for function instances, Function Compute calls the corresponding hook when a related instance lifecycle event occurs. Initializer and PreStop hooks can be configured for function instances in Custom Container runtimes. For more information, see Function instance lifecycles.

The following table describes the method to implement the Initializer hook. The method to implement the PreStop hook is the same.


Input request

Expected response

(Optional) POST /initialize

Request body: none.

Request header: common request headers. For more information, see Common request headers in Function Compute.

Response body: the return value of Initializer.


  • 200: succeeded

  • 404: failed

Sample code of initialize in Python:

@app.route('/initialize', methods=['POST'])
def init_invoke():
  rid = request.headers.get(x-fc-request-id)
  print("FC Initialize Start RequestId: " + rid)
  # do your things
  print("FC Initialize End RequestId: " + rid)
  return "OK"

(Optional) GET /pre-stop

Response body: the return value of the PreStop hook.


  • 200: succeeded

  • 404: failed

If you want to use the Initializer hook in a custom runtime, you must implement the corresponding logic with the /initialize path and the POST method in your HTTP server. You can use the sample code for initialize in the preceding table as references.


If you do not configure Initializer when you create a function, you do not need to implement /initialize. In this case, even if the HTTP Server implements /initialize, the /initialize logic in the code cannot be called and executed.