All Products
Search
Document Center

Function Compute:ALB triggers

Last Updated:Dec 04, 2025

Function Compute supports Application Load Balancer (ALB) as an event source. This lets you set Function Compute as a backend service for an ALB instance. ALB forwards requests to functions in Function Compute and synchronously returns the function invocation results. This topic describes how to configure an ALB trigger to invoke a function.

Prerequisites

Limits

  • The ALB instance and the Function Compute service must be in the same region.

  • The following limits apply when you use Function Compute as a backend server for ALB:

    • Only functions that are Triggered By HTTP Requests are supported.

    • When a client accesses a function, Function Compute requires the HTTP request header to contain the Date field. The Date field indicates the time when the message was sent.

  • The following limits apply when you use Function Compute with ALB:

    • In ALB, you must set the server group type to Function Compute. The backend server group and the listener support only the HTTP protocol.

    • ALB supports adding only one function as a backend server.

Step 1: Write and test the function code

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

  2. In the top navigation bar, select a region. On the Services page, click the desired service.

  3. On the Functions page, click the name of the desired function.

  4. On the function details page, click the Function Code tab. Write code in the code editor and then click Deploy Code.

    The following code provides an example.

    var getRawBody = require('raw-body');
    var getFormBody = require('body/form');
    var body = require('body');
    
    
    /*
    To enable the initializer feature
    please implement the initializer function as below: 
    exports.initializer = (context, callback) => {
      console.log('initializing');
      callback(null, '');
    };
    */
    
    exports.handler = (req, resp, context) => {
        console.log('hello world');
    
        var params = {
            path: req.path,
            queries: req.queries,
            headers: req.headers,
            method : req.method,
            requestURI : req.url,
            clientIP : req.clientIP,
            log : ("Hello World.This is FC!"),
        }
    
        getRawBody(req, function(err, body) {
            for (var key in req.queries) {
              var value = req.queries[key];
              resp.setHeader(key, value);
            }
            resp.setHeader("Content-Type", "text/plain");
            params.body = body.toString();
            resp.send(JSON.stringify(params, null, '    '));
        });
    
        /*
        getFormBody(req, function(err, formBody) {
            for (var key in req.queries) {
              var value = req.queries[key];
              resp.setHeader(key, value);
            }
            params.body = formBody;
            console.log(formBody);
            resp.send(JSON.stringify(params));
        });
        */
    }
  5. On the Function Code tab, click Test Function.

    After the function is executed, view the result that appears above the Function Code tab.

Step 2: Create a server group of the Function Compute type

  1. Log on to the ALB console.

  2. Go to the ALB console, in the left-side navigation pane, choose Server Groups. In the top navigation bar, select the same region as the created function, then click Create Server Group.

  3. In the navigation pane on the left, choose Application Load Balancer ALB > Server Groups.

  4. On the Server Groups page, click Create Server Group, configure the following parameters, and then click Create.

    函数计算类型服务器组

    Parameter

    Description

    Server Group Type

    Select the server group type. In this example, select Function Compute.

    Server Group Name

    Custom

    Select Backend Protocol

    Select a backend protocol. In this example, select HTTP.

    Select Resource Group

    Select the resource group to which the server group belongs.

    Configure Health Check

    Enable Health Check

    Enable or disable health checks. In this example, the default setting is used, which is to disable health checks.

    Advanced Configuration

    To enable health checks, configure the advanced settings as needed. For more information, see Create and manage a server group.

  5. In the message that is displayed, click Add Backend Server.

  6. On the Backend Servers tab, click Set Function Compute.

  7. In the Add Backend Server panel, select one of the following methods to configure the backend server, and then click OK.

    • By Selecting Resource

      Parameter

      Description

      Configuration Method

      Select By Selecting Resource.

      Service

      Select the service to which the target function belongs.

      Version

      Select LATEST. A newly created service has only the LATEST version by default.

      Function

      Select the target function.

      Notes

      Custom

    • By ARN

      Parameter

      Description

      Configuration Method

      Select By ARN.

      ARN

      Enter the ARN of the target function. For information about how to obtain the ARN of a function, see Obtain the ARN of a function.

      Notes

      Custom

    The Add Backend Server panel displays the Added successfully! message. Click Close. You can then view the configured backend server on the Backend Servers tab.

Step 3: Create an ALB instance and configure a listener

  1. Log on to the Application Load Balancer (ALB) console.

  2. In the top navigation bar, select a region. In this example, China (Hangzhou) is selected.

  3. On the Instances page, click Create Application Load Balancer.

  4. On the Application Load Balancer (Pay-As-You-Go) purchase page, configure the parameters.

    This section describes only the parameters that are relevant to this topic. For more information about other parameters, see Create and manage an ALB instance.

    • Region: In this example, select China (Hangzhou).

    • Instance Network Type: In this example, select Internet.

  5. On the Instances page, find the created ALB instance. In the Actions column of the target instance, click Create Listener to open the listener configuration wizard.

  6. In the Configure Listener wizard, specify the following parameters and click Next.

    Parameter

    Description

    Select Load Balancer Protocol

    Select the protocol type for the listener. In this example, select HTTP.

    Listener Port

    Enter the listener port that is used to receive requests and forward them to backend servers. The port range is 1 to 65535. In this example, enter 80.

    Listener Name

    Custom

    Advanced Configuration

    In this example, the default configurations are used.

  7. In the Select Server Group wizard, select Function Compute from the Select Server Group drop-down list. Select the backend server group that you created in Step 2: Create a server group of the Function Compute type, and then click Next.

  8. In the Configuration Review wizard, confirm the configurations and click Submit.

Step 4: Test the connectivity

After you complete the preceding steps, a connection is established between Function Compute and Application Load Balancer (ALB). Perform the following steps to test the connectivity between Function Compute and ALB.

  1. Open the command line and run the curl -v command to obtain the Date header.

    Function Compute requires that the HTTP request header contains the Date field.

    curl -v <Domain name of the ALB instance>

    获取时间

  2. Run the curl -H command to access the domain name of the ALB instance.

    curl -H "Date header" <Domain name of the ALB instance>

    As shown in the following figure, if you receive the following response message, it indicates that ALB can forward requests to Function Compute and successfully invoke the function.ALB支持函数计算验证结果

More information

In addition to the Function Compute console, you can configure triggers by using the following methods:
  • Use Serverless Devs to configure triggers. For more information, see Serverless Devs.
  • Use SDKs to configure triggers. For more information, see SDKs.

To modify or delete a trigger, see Manage triggers.