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
- Function Compute
- Create a service
- A function is created. For more information, see Manage services.
- Application Load Balancer (ALB)
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
- Log on to the Function Compute console. In the left-side navigation pane, click Services & Functions.
- In the top navigation bar, select a region. On the Services page, click the desired service.
- On the Functions page, click the name of the desired function.
- 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)); }); */ }
- 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
- 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, configure the parameters, and then click Create. The following table describes the parameters:
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 custom name for the server group. 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 the health check feature. In this example, health checks are disabled. This is the default setting. Advanced Settings If 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. - 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, 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
Parameter Description Configuration Method Select Service from the drop-down list. Service Select the service to which the function belongs. Version Select LATEST. By default, a new service runs only the latest version. Function Select the function that you want to add. Description Enter a custom description. - Specify an Alibaba Cloud Resource Name (ARN) to add a function
Parameter Description Configuration Method Select ARN from the drop-down list. ARN Enter 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. Description Enter 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. - Specify a service to add a function
Step 3: 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.
- In the Configure Listener step, configure the parameters and click Next. The following table describes the parameters:
Parameter Description Listener Protocol Select a listening protocol. In this example, HTTP is selected. Listener Port Specify 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 Name Enter a custom name for the listener. Advanced Settings In this example, the default advanced settings are used. - 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.
- 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:
- 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.
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.