This topic describes how to specify a function from Function Compute as a backend
server of Application Load Balancer (ALB). After you add a function to an ALB server group, requests received by the server group are forwarded to Function Compute
to invoke the specified function. After Function Compute runs the function, ALB returns the response from Function Compute to the client.
Background information
Alibaba Cloud Function Compute is a fully managed event-driven compute service. Function
Compute allows you to run codes without the need to purchase and manage infrastructure
resources such as servers. After you create a function in Function Compute, you can
specify the function as a backend server of an ALB instance by adding the function to a server group of the Function type and associating
the server group with a listener of the ALB instance. ALB forwards HTTP requests to Function Compute to invoke the specified function. After
Function Compute runs the function, the response is returned to the client. For more
information about Function Compute, see What is Function Compute?.
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.
Procedure
Step 1: Create a Function Compute service
- Log on to the Function Compute console.
- In the left-side navigation pane, click Services and Functions.
- In the top navigation bar, select the region where you want to create the Function
Compute service. In this example, China (Hangzhou) is selected.
- On the Services page, click Create Service.
- In the Create Service panel, set the following parameters and click OK.
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 (-). The name 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.
After you enable the logging feature, Function Compute adds the AliyunFCDefaultRole role to the Function Compute service. This way, the Function Compute service can
access Log Service. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.
- Disable: If you select this option, you cannot use Log Service to store or query the logs
of the function.
Note Make sure that Log Service is activated. If Log Service is not activated, you cannot
enable the logging feature. For more information, see Enable Log Service.
|
Tracing Analysis |
Specify whether to enable Tracing Analysis. In this example, Disable is selected.
- Enable: After you enable Tracing Analysis, you can use the Jaeger distributed tracing system
to upload trace information. This way, you can view the traces of the function. You
can also analyze and perform diagnostics on performance bottlenecks in a Serverless
architecture. For more information, see Overview.
- Disable: disables Tracing Analysis.
|
Step 2: Create a function
- On the Services page, find the service that you want to manage and click Functions in the Actions column.
- On the Functions page, click Create Function.
- On the Create Function page, select a method for creating the function as needed.
In this example, Start from Scratch is selected.
- In the Basic Settings and Configure Trigger sections, set the following parameters and click Create.
Parameter |
Description |
Basic Settings |
Function Name |
Enter a name for the function. |
Runtime Environments |
Select a runtime environment. In this example, Node.js 12 is selected.
For more information about the runtime environments that are supported by Function
Compute, see Manage functions.
|
Function Trigger Mode |
Select a trigger mode. In this example, HTTP Request-triggered is selected.
HTTP Request-triggered: If you select this option, the function is triggered by an HTTP request. This mode
allows you to develop web services in an efficient manner.
Note ALB supports only a function that is triggered by HTTP requests.
|
Instance Category |
Select an instance type. In this example, Elastic Instance is selected.
- Elastic Instance
- Performance Instance
For more information, see Instance types and instance modes.
|
Memory Capacity |
Specify the memory that is used to run the function. |
Configure Trigger |
Trigger Type |
HTTP Trigger is displayed.
A trigger specifies how a function is invoked.
|
Name |
Enter a name for the trigger. |
Authentication |
Specify whether authentication is required or allow anonymous access. In this example,
Yes is selected.
- Yes: Authentication is required and anonymous access is not allowed. ALB checks the identity of the client that sends a request to ensure security. .
- No: Authentication is not required and anonymous access is allowed. If you select this
option, security is compromised. All users can send HTTP requests to invoke the function.
|
Request Method |
Select the request methods that can be used to invoke the function. In this example,
GET and POST are selected.
|
- Click the Code tab and modify the code in the code editor.
The following code block is used as an example:
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!"), }
- On the Code tab, click Deploy and then click Test Function.
Step 3: Create a server group of the Function type
- Log on to the ALB console.
- In the top navigation bar, select the region where you want to create the server group.
In this example, China (Hangzhou) is selected.
- In the left-side navigation pane, choose .
- On the Server Groups 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. The name must be 2 to 128 characters in length,
and can contain letters, digits, periods (.), underscores (_), and hyphens (-). The
name must start with a letter.
|
Backend Server Protocol |
Select a backend protocol. In this example, HTTP is selected.
|
Resource Group |
Select the resource group to which the server group belongs. |
Configure Health Check |
Enable Health Check |
Specify whether to enable health checks. In this example, health checks are disabled.
This is the default setting.
|
Advanced Settings |
If you enable health checks, you can modify the parameters in the Advanced Settings
section based on your business requirements. For more information, see Manage server groups.
|
- On the Server Groups page, find the server group that you created and click Modify Backend Server in the Actions column.
- On the Backend Servers tab, click Add Function.
- In the Add Backend Server panel, add a function by using one of the following methods, set the required parameters,
and then click OK.
- Add a function by specifying Service
Parameter |
Description |
Configuration Mode |
Select Service from the drop-down list.
|
Service |
Select the Function Compute service that you create 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 ARN
Parameter |
Description |
Configuration Mode |
Select ARN from the drop-down list.
|
ARN |
Enter the Alibaba Cloud Resource Name (ARN) of the function that you want to add.
You can obtain 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
- Log on to the ALB console.
- In the top navigation bar, select the region where you want to create the ALB instance.
In this example, China (Hangzhou) is selected.
- On the Instances page, click Create ALB.
- 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.
- 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.
- On the Configure Listener wizard page, set the following parameters and click Next.
Parameter |
Description |
Listener Protocol |
Select the protocol of the listener. In this example, HTTP is selected.
|
Listener Port |
Specify the port on which the ALB instance listens. The ALB instance listens on the
port and forwards requests to backend servers. Valid values: 1 to 65535. In this example,
80 is specified.
|
Listener Name |
Specify a name for the listener. |
Advanced Settings |
In this example, the default advanced settings are used. |
- On the Select Server Group wizard page, select Function from the Server Group drop-down list, select the server group that you create in Step 3, and then click Next.
- On the Confirm wizard page, confirm the configurations and click Submit.
Step 5: Test the connectivity
After you complete the preceding operations, a connection is established between the
ALB instance and Function Compute. You can perform the following operations to test the
connectivity between the ALB instance and Function Compute.
- 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>

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

Related topics
Operations in Function Compute
- For more information about how to modify a function, see Update a function.
- For more information about how to delete a function, see Delete a function.
- For more information about how to modify a Function Compute service, see Update a Service.
- For more information about how to delete a Function Compute service, see Delete a service.