All Products
Search
Document Center

Server Load Balancer:Specify a function from Function Compute as a backend server of ALB

Last Updated:Feb 22, 2024

This topic describes how to specify a function from Function Compute as a backend server of Application Load Balancer (ALB). The function is used to distribute requests. You can also create a server group of the Function Compute type for your ALB instance. After you add a function to an ALB server group, requests to ALB are forwarded to Function Compute and invoke the function. After Function Compute runs the function, the responses are returned to the requesters.

Important

ALB server groups of the Function Compute type support Function Compute 2.0. Function Compute 3.0 is not supported.

The functions used in this topic are from Function Compute 2.0.

Background information

Alibaba Cloud Function Compute is a fully managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure resources such as servers. Function Compute allocates computing resources, runs code in an elastic and reliable way, and provides features such as log query, performance monitoring, and alerting. Function Compute is an event-driven computing service that uses a serverless architecture. Function Compute allows you to write and upload code without the need to manage infrastructure resources. With Function Compute, you can quickly create any type of applications and services and only pay for the resources actually consumed when you run your code.

After you create a function, you can specify the function as a backend server of ALB by configuring a listener and a server group. When ALB receives a request, the request is forwarded to Function Compute and invokes the function. After Function Compute runs the function, the response is returned to the requester.

Note

You must activate Function Compute before you can use it.

Limits

  • The ALB instance and function must be in the same region.

  • Take note of the following limits when you specify a function as a backend server of ALB:

    • You can add only a function that processes HTTP requests.

    • The Date header must be included in the requests that are forwarded to Function Compute. The Date header specifies the time when a request is sent.

  • Each server group of the Function Compute type supports only one function.

Step 1: Create a Function Compute service

  1. Log on to the Function Compute console.

    Important

    If you are redirected to the console of Function Compute 3.0, click Back to Function Compute 2.0 in the upper-left corner to go to the console of Function Compute 2.0.

  2. In the left-side navigation pane, click Services and Functions.

  3. In the top navigation bar, select the region where you want to create the Function Compute service. In this example, China (Hangzhou) is selected.

  4. On the Services page, click Create Service to create a service.

  5. In the Create Service panel, set the following parameters and click OK. Parameters that are not listed in the following table can use the default values.

    Parameter

    Description

    Name

    Enter a name for the Function Compute service.

    The name must be 1 to 128 characters in length, and can contain letters, digits, underscores (_), and hyphens (-). It cannot start with a digit or a hyphen (-).

    Description

    Enter a description for the Function Compute service.

    Logging

    Specify whether to enable the logging feature. In this example, Enable is selected.

    • Enable: After you enable the logging feature, Function Compute automatically creates a project and a Logstore and delivers the logs of the function to the Logstore.

      In this case, Function Compute grants the service the AliyunFCDefaultRole role, which is the minimum permission required for the service to access Simple Log Service. For more information about the Resource Access Management (RAM) roles of Function Compute, see Grant Function Compute permissions to access other Alibaba Cloud services.

    • Disable: If you select this option, you cannot use Simple Log Service to store or query the logs of the function.

    Note

    Make sure that Simple Log Service is activated. Otherwise, you cannot enable the logging feature.

Step 2: Create a function

  1. On the Services page, find the service that you want to manage and click Functions in the Actions column.

  2. On the Functions page, click Create Function.

  3. On the Create Function page, select a method to create a function based on your business requirements.

    In this example, Use Built-in Runtime is selected.

  4. In the Basic Settings, Code, and Trigger Configurations sections, set the following parameters and click Create.

    The following table describes only the key parameters. Use the default values for other parameters.

    Parameter

    Description

    Basic Settings

    Function Name

    Enter a name for the function.

    Handler Type

    Select a handler type. In this example, HTTP Handler is selected.

    Handler Type: specifies that the function processes HTTP requests or WebSocket requests.

    Note

    ALB supports only HTTP handlers.

    Code

    Runtime

    Select a runtime environment. In this example, Node.js 14 is selected.

    For more information about runtime environments supported by Function Compute, see Manage functions.

    Code Upload Method

    Select a method to upload the function code to Function Compute.

    In this example, Use Sample Code is selected.

    Trigger Configurations

    Trigger Type

    The selected type of trigger is displayed. In this example, HTTP Trigger is used.

    A trigger specifies how a function is invoked.

    Name

    Enter a name for the trigger.

    Request Method

    Select the request methods that can be used to invoke the function. In this example, GET and POST are selected.

    Authentication Method

    In this example, Signature Authentication is selected. For more information, see Signature authentication.

  5. Click the Code tab and modify the code in the code editor.

    The following code block is used as an example:

    varparams={
    	path : req.path,
     	queries : req.queries,
    	headers : req.headers,
    	method : req.method,
    	requestURI : req.url,
    	clientIP : req.clientIP,
    	log : ("Hello World.This is FC!"), 
    }
  6. On the Code tab, click Deployment Code in the upper-left corner and click Test Function.

    You can view the result in the Response section after Function Compute runs the function. For more information, see Quickly create a function.

Step 3: Create a server group of the Function 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 Group.

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

    Parameter

    Description

    Server Group Type

    Select the type of server group that you want to create. In this example, Function Compute is selected.

    Server Group Name

    Enter a name for the server group.

    Health Check Settings

    Enable Health Check

    Specify whether to enable health checks. In this example, health checks are disabled. This is the default setting.

  5. On the Server Groups page, find the server group that you want to manage 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 and click OK.

    • Add a function by specifying a service

      Parameter

      Description

      Configuration Method

      Select Service.

      Service

      Select the Function Compute service created in Step 1.

      Version

      Select LATEST. By default, a newly created service runs only the LATEST version.

      Function

      Select the function that you created from the drop-down list. If no function is available, click Create a function to create a function. For more information, see Manage functions.

      Description

      Enter a description.

    • Add a function by specifying an Alibaba Cloud Resource Name (ARN)

      Parameter

      Description

      Configuration Method

      Select ARN from the drop-down list.

      ARN

      Enter the ARN of the function that you want to add.

      You can copy the ARN of a function on the details page of the function in the Function Compute console. For more information, see Obtain the ARN of a function.

      Description

      Enter a description.

Step 4: 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 Application Load Balancer buy page, set the parameters and click Buy Now.

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

    • Region: the region in which you want to create the ALB instance. In this example, China (Hangzhou) is selected.

    • Network Type: the network type of the ALB instance. In this example, Internet-facing is selected.

  5. On the Instances page, find the ALB instance that you created and click Create Listener in the Actions column.

  6. In the Configure Listener step, configure the following parameters and click Next.

    Parameter

    Description

    Select a listener protocol.

    Select a listener protocol. In this example, HTTP is selected.

    Listener Port

    Enter 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 used.

    Listener Name

    Enter a name for the listener.

    Health Check Settings

    In this example, the default advanced settings are used.

  7. In the Server Group step, select Function Compute from the Server Group drop-down list, select the backend server group created in Step 3, and then click Next.

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

Step 5: Test network connectivity

After you complete the preceding steps, ALB is connected to Function Compute. You can perform the following operations to test the network connectivity between ALB and Function Compute.

  1. Open the command-line interface (CLI) and run the curl -v <ALB instance domain name> command to query the Date header.

    The Date header must be included in HTTP requests to be processed by Function Compute.

    image.png

  2. Run the curl -H "Date header" <ALB instance domain name> command to access the domain name of the ALB instance.

    If you receive an echo reply packet, it indicates that the ALB instance can forward requests to Function Compute and invoke the function, as shown in the following figure.ALB支持函数计算验证结果

References

Function Compute 2.0 documentation:

ALB documentation: