Function Compute supports Application Load Balancer (ALB) as an event source to trigger function execution. You can configure Function Compute as the backend service of ALB. ALB forwards requests to functions in Function Compute and synchronously returns the result of function invocation. This topic describes how to configure an ALB trigger to trigger function execution.

Prerequisites

Limits

  • The ALB instance and Function Compute service must belong to the same region.
  • When you add a function to a server group of ALB, take note of the following limits:
    • You can add only a function that is triggered by HTTP requests.
    • The Date header must be carried in the requests sent to Function Compute. The Date header specifies the time when a request is sent.
  • Take note of the following rules before you configure ALB to send requests to Function Compute:
    • You must create a service group of the Function Type for ALB. In addition, you must set the backend protocol and the protocol of the listener that is associated with the server group to HTTP.
    • You can specify only one function for an ALB instance.

Step 1: Write the function code and test the function

  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 Code tab, edit the function code in the code editor, and then click Deploy.
    The following code provides an example on how to write the function code:
    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. Click the Code tab and click Test Function.
    After the function is executed, you can view the result on the Code tab.

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

  1. Log on to the ALB console.
  2. In the top navigation bar, select the region where you want to create the server group. In this example, China (Hangzhou) is selected.
  3. In the left-side navigation pane, choose ALB > Server Groups.
  4. On the Server Groups page, click Create Server Group, configure the parameters, and then click Create. The following table describes the parameters:
    Server group of the Function Compute type
    ParameterDescription
    Server Group TypeSelect the type of server group that you want to create. In this example, Function Compute is selected.
    Server Group NameEnter a custom name for the server group.
    Backend Server ProtocolSelect a backend protocol. In this example, HTTP is selected.
    Resource GroupSelect the resource group to which the server group belongs.
    Configure Health Check
    Enable Health CheckSpecify whether to enable the health check feature. In this example, health checks are disabled. This is the default setting.
    Advanced SettingsIf you enable the health check feature, you can modify the parameters in the Advanced Settings section based on your business requirements. For more information, see Create and manage a server group.
  5. On the Server Groups page, find the server group that you created and click Modify Backend Server in the Actions column.
  6. On the Backend Servers tab, click Add Function.
  7. In the Add Backend Server panel, use one of the following methods to add a function, configure the required parameters, and then click OK. The following table describes the parameters:
    • Specify a service to add a function
      ParameterDescription
      Configuration MethodSelect Service from the drop-down list.
      ServiceSelect the service to which the function belongs.
      VersionSelect LATEST. By default, a new service runs only the latest version.
      FunctionSelect the function that you want to add.
      DescriptionEnter a custom description.
    • Specify an Alibaba Cloud Resource Name (ARN) to add a function
      ParameterDescription
      Configuration MethodSelect ARN from the drop-down list.
      ARNEnter the ARN of the function that you want to add. For information about how to obtain the ARN of a function, see Obtain the ARN of a function.
      DescriptionEnter a custom description.
    If the function is added, Added is displayed on the Add Backend Server panel. Click Close. You can 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 ALB console.
  2. In the top navigation bar, select the region where you want to create the ALB instance. In this example, China (Hangzhou) is selected.
  3. On the Instances page, click Create ALB.
  4. On the ALB (Pay-As-You-Go) International Site page, set the parameters.
    The following section describes the parameters that are relevant to this topic. For more information about the other parameters, see Create an ALB instance.
    • Region: In this example, China (Hangzhou) is selected.
    • Network Type: In this example, Public-facing is selected.
  5. On the Instances page, find the ALB instance that you created and click Create Listener in the Actions column to open the listener configuration wizard.
  6. In the Configure Listener step, configure the parameters and click Next. The following table describes the parameters:
    ParameterDescription
    Listener ProtocolSelect a listening protocol. In this example, HTTP is selected.
    Listener PortSpecify the port on which the ALB instance listens. The ALB instance listens for requests on the specified port and then forwards the requests to backend servers. Valid values: 1 to 65535. In this example, 80 is specified.
    Listener NameEnter a custom name for the listener.
    Advanced SettingsIn this example, the default advanced settings are used.
  7. In the Select Server Group step, select Function Compute from the Server Group drop-down list, select the server group that you created in Step 2: Create a server group of the Function Compute type, and then click Next.
  8. On the Confirm wizard page, confirm the configurations and click Submit.

Step 4: Test the network connectivity

After you preform the preceding operations, a connection is established between Function Compute and ALB. To test the connectivity between Function Compute and ALB, perform the following steps:

  1. Open the CLI and run the curl - v command to retrieve the Date header.
    The Date header must be carried in HTTP requests to be processed by Function Compute.
    curl -v <Domain name of the ALB instance>
    Retrieve the Date header
  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>
    If you receive a response as shown in the following figure, it indicates that ALB has sent the request to Function Compute and invoked the specified function. Check whether the function works as expected

References

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 Trigger management.