All Products
Search
Document Center

Function Compute:Configure instance lifecycles

Last Updated:Jun 20, 2026

When handling user requests, Function Compute automatically allocates one or more instances. Each instance provides a secure and isolated runtime environment. When you migrate traditional applications to a serverless architecture, the transient nature of instance creation and destruction can cause issues such as delayed monitoring data updates, metric latency, or data loss. To address these issues, Function Compute provides instance lifecycle hooks to help ensure your monitoring data is timely and complete.

Function instance lifecycle

Function instances are created and destroyed on demand based on your function's current invocation volume. The lifecycle of each function instance includes three phases: instance creation, invocation, and instance destruction, as shown in the following figure.

image

Creating

During the Creating phase, Function Compute creates a function instance based on your function's configuration. In this phase, Function Compute performs the following tasks in order:

  1. Instance Create: This includes loading code, loading layers or pulling an image, and starting the instance.

  2. Runtime Init.

  3. Execute the configured Initializer hook. For more information, see Initializer hook.

image

Instance creation typically occurs in the following two scenarios.

  • Elastic scale-out

    When an invocation request is received and all current function instances are busy, a new instance is created to handle the request. The instance is created immediately, and then the invocation process begins. This elastic scale-out can cause a cold start. For information about how to mitigate this issue, see Best practices for optimizing cold starts in Function Compute.

  • Minimum number of instances adjustment

    If you change the configured minimum number of instances from 0 to 1 or more, Function Compute immediately starts the instance creation process. If no invocation requests are received at that time, there might be a delay between the instance creation and the first invocation. For more information, see Configure an elastic policy for the minimum number of instances.

Invoke

While an instance is running, Function Compute calls your function handler to process invocation requests. During the invocation phase, for a built-in runtime, one instance handles only one request at a time. For a custom runtime or custom container runtime, one instance can handle multiple requests concurrently. You can achieve this by configuring instance concurrency. For more information, see Configure instance concurrency.

Function Compute only bills for the time when your code is executing, including requests and hook handlers. During idle periods between requests, the instance is frozen and does not incur charges. For more information, see Billing.

Destroy

This phase is triggered if a function instance does not receive any invocations for a certain period. During the destruction phase, Function Compute first runs the PreStop hook. You can perform cleanup tasks in this hook.

Instance destruction typically occurs in the following three scenarios.

  • Idle instance: If an instance remains idle for a specified period, Function Compute automatically reclaims it.

  • Minimum number of instances adjustment: When you scale in the minimum number of instances, Function Compute immediately destroys the surplus instances.

  • Instance exception: If an instance encounters an error during the instance creation or invocation phase, Function Compute destroys it.

Instance freeze mechanism

When there are no invocation requests, Function Compute freezes the instance. When a new request arrives, Function Compute thaws the instance. This process is shown in the following figure.

image

An instance freeze occurs mainly in the following two situations:

  • After the instance initialization phase is complete but before the first invocation.

  • After an invocation completes but before the next one begins.

After an invocation completes, Function Compute freezes the function instance. Any background processes, threads, or coroutines in your code are paused and cannot continue. This can prevent asynchronous logs from being written successfully.

Limitations

  • For GPU-accelerated functions, the Initializer hook supports two types: Code Invocation and Command Execution. You can enable only one of these types at a time.

  • For event-triggered, web, and task functions, the Initializer hook supports only the Code Invocation type.

  • The PreStop hook supports only the Code Invocation type, and all runtimes support the PreStop hook.

  • The input parameters for a PreStop hook method do not include an event parameter.

  • The PreStop hook does not return a value, and any return logic in your function is ignored.

  • If you use the Java runtime, you must update fc-java-core to version 1.4.0 or later. Otherwise, you cannot use the PreStop hook.

  • Do not assume that asynchronous operations, such as background threads or log flushing, will complete before the instance freezes.

  • The configuration method for Code Invocation hooks varies by runtime. For a built-in runtime, you must define a custom entry point when you configure a lifecycle hook. For example, if you set the Initializer hook handler to index.initialize, you must add an initialize hook function to your code. In contrast, for custom and custom container runtimes, after you configure the Initializer and PreStop hooks, the system sends an HTTP request (POST /initialize or GET /pre-stop) to your function when the instance starts or stops. Your application code must handle these requests.

Billing

Invocations of instance lifecycle hooks do not incur request charges. Other fees are calculated using the same logic as the instance invocation phase. The billable duration is illustrated in the following figure. For more information about billing, see Billing overview.

image

Prerequisites

A function has been created. For more information, see Create a function.

Configure instance lifecycle hooks

Console

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the function details page, click the Configuration tab, and then click Instance Configuration in the Modify section.

  4. In the Instance Configuration panel, configure the Initializer hook and its timeout period.

    This topic uses a GPU-accelerated function as an example to describe how to configure an Initializer hook. For event-triggered, web, and task functions, the Initializer hook supports only the Code Invocation type. You can follow the steps for the Code Invocation type described in this topic.

    • Select the Code Invocation type

      Turn on the Initializer hook switch and set the Initializer hook timeout to 300 seconds. After you enable this feature, Function Compute sends an HTTP POST /initialize request to the function when an instance starts. A 200 response indicates success, while a 4xx or 5xx response causes an error or an instance restart.

    • Select the Command Execution type

      Turn on the Initializer hook switch, set the Initializer hook timeout to 300 seconds, and set Command to >/bin/sh. In the code editor, write a shell script to send a health check POST request to the local Stable Diffusion API. Set the URL to http://localhost:7860 and the REQUEST_PATH to sdapi/v1/txt2img. Construct a JSON payload that includes the prompt, steps, height, and width fields, and then use curl to send the request.

  5. In the Instance Configuration panel, configure the PreStop hook and its timeout period, and then click Deploy.

  6. If you configured a hook of the Code Invocation type, you must implement the corresponding function in your code.

    1. Click the Code tab. In the code editor, add the logic for the hook function.

      For example, if you configured the PreStop hook handler as index.preStop, you must implement the preStop function in your code. For information about how to implement instance lifecycle hooks for different runtimes, see Lifecycle hooks for function instances.

      Note

      The online IDE supports PHP, Python, Node.js, and custom runtimes, but does not support compiled languages such as Java, Go, and .NET, or custom container runtimes.

    2. Above the code editor, click Deploy, and then click Test Function.

Serverless Devs

When you use Serverless Devs to configure an Initializer hook, the code snippet in your s.yaml file is similar to the following example:

  codeUri: './code.zip'
  ......
  instanceLifecycleConfig:
    initializer:
      timeout: 60
      command:
        - /bin/sh
        - -c
        - echo "hello"

If you need to disable a hook, you must explicitly set its handler and command parameters to empty strings. This is required because the backend does not otherwise apply the update. For example, to disable the Initializer hook, deploy the following configuration. The timeout and command parameters for the Initializer hook are then ignored.

  codeUri: './code.zip'
  ......
  instanceLifecycleConfig:
    initializer:
      handler: ""
      timeout: 60
      command: ""

For information about how to implement instance lifecycle hooks for different runtimes, see Lifecycle hooks for function instances.

SDK

You can use an SDK to deploy and update hooks. This section describes how to get sample SDK code for configuring hooks when you create a function.

  1. Go to the CreateFunction API reference topic and click Debug to open the OpenAPI Portal.

  2. On the Parameters tab, specify the input parameters based on the function that you want to create.

    Note

    The command parameter is not supported for PreStop hooks.

    Expand instanceLifecycleConfig. In the initializer section, set timeout to 300. In the command list, add an item at index 0 with the value curl -X POST 'http://localhost:9000/preWarm'. In the preStop section, set handler to index.preStop and timeout to 3.

  3. After you configure the parameters, click the SDK Example tab to get the sample code for your preferred language.

For information about how to implement instance lifecycle hooks for different runtimes, see Lifecycle hooks for function instances.

Lifecycle hooks for function instances

All runtimes in Function Compute support both Initializer and PreStop hooks. Refer to the following documentation to learn how to implement instance lifecycle hooks in different runtimes.

Runtime

Description

References

Node.js

Implement lifecycle hooks for function instances in Node.js.

Lifecycle hooks for function instances

Python

Implement lifecycle hooks for function instances in Python.

Lifecycle hooks for function instances

PHP

Implement lifecycle hooks for function instances in PHP.

Lifecycle hooks for function instances

Java

Implement lifecycle hooks for function instances in Java.

Lifecycle hooks for function instances

C#

Implement lifecycle hooks for function instances in C#.

Lifecycle hooks for function instances

Go

Implement lifecycle hooks for function instances in Go.

Lifecycle hooks for function instances

custom runtime

Implement lifecycle hooks for function instances in a custom runtime.

Lifecycle hooks for function instances

custom container runtime

Implement lifecycle hooks for function instances in a custom container runtime.

Lifecycle hooks for function instances

Query logs related to hooks

After you configure instance lifecycle hooks and implement the corresponding functions, you can query the related logs.

Note

Logs generated by hooks of the Command Execution type cannot be written to function logs currently.

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the function that you want to manage.

  3. On the function details page, click the Logs tab. On the Invocation Requests tab, find the target request and click Advanced Logs in the Actions column.

    You can use the instance ID to query the start and end logs for all lifecycle hooks. You can also use a query like instance ID AND "hook keyword" to find logs for a specific hook, for example, c-62833f38-20f1629801fa4bd***** and PreStop.

    You can also use the RequestId from the start and end logs to query for logs related to a specific request. If a log does not contain a RequestId, click the db_lifecycle_log_context icon in that log entry to view its context logs.