Function Compute supports Application Load Balancer (ALB) as an event source. You can set Function Compute as a backend service for ALB. ALB forwards requests to functions in Function Compute and returns the function invocation results synchronously. This topic describes how to configure an ALB trigger to execute a function.
Prerequisites
Function Compute
Application Load Balancer (ALB)
Limits
The ALB instance and the Function Compute service must be in the same region.
Limits on using Function Compute as a backend server for ALB:
You can create only functions that are triggered by HTTP Requests.
Function Compute requires that the HTTP request header from a client includes the Date field. The Date field indicates the time when the message was sent.
Limits on using Function Compute with ALB:
In ALB, the server group type must be set to Function Compute. Both the backend server group and the listener support only the HTTP protocol.
ALB supports only one function as a backend server.
Step 1: Write and test the function code
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, write your code in the editor, and then click Deploy Code.
The following code is 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)); }); */ }
On the Code tab, click Test Function.
After the execution is complete, the result is displayed at the top of the Code tab.
Step 2: Create a server group of the Function Compute type
Log on to the ALB console.
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.
In the navigation pane on the left, choose .
On the Server Groups page, click Create Server Group, set the following parameters, and then click Create.
Configuration item
Description
Server Group Type
Select a server group type. This topic uses Function Compute as an example.
Server Group Name
Enter a custom name.
Backend Protocol
Select a backend protocol. This topic uses HTTP as an example.
Resource Group
Select a resource group.
Configure Health Check
Enable Health Check
Enable or disable health checks. This topic uses the default setting, which is to disable health checks.
Advanced Configuration
If you want to enable health checks, configure the parameters in the Advanced Configuration section as needed. For more information, see Create and manage server groups.
In the message that is displayed, click Add Backend Server.
On the Backend Server tab, click Set Function Compute.
In the Add Backend Server panel, add a function using one of the following methods and then click OK.
By Resource
Configuration item
Description
Configuration Method
Select By Resource.
Service
Select the service to which the target function belongs.
Version
Select LATEST. By default, a new service has only the LATEST version.
Function
Select the target function.
Notes
Custom
By ARN
Configuration item
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
Enter custom notes.
The message Added successfully! appears in the Add Backend Server panel. Click Close. The configured backend server is displayed on the Backend Servers tab.
Step 3: Create an ALB instance and configure a listener
Log on to the Application Load Balancer (ALB) console.
In the top navigation bar, select a region. This topic uses China (Hangzhou) as an example.
On the Instances page, click Create Application Load Balancer.
On the Application Load Balancer (Pay-As-You-Go) purchase page, set the parameters.
Only the parameters that are relevant to this topic are described here. For more information about other parameters, see Create and manage an ALB instance.
Region: This topic uses China (Hangzhou) as an example.
Instance Network Type: This topic uses Internet as an example.
On the Instances page, find the ALB instance that you created and click Create Listener in the Actions column.
In the Configure Listener wizard, set the following parameters and click Next.
Configuration item
Description
Select Load Balancing Protocol
Select a listener protocol. This topic uses HTTP as an example.
Listener Port
Enter the listener port that is used to receive requests and forward them to backend servers. The port must be an integer from 1 to 65535. This topic uses 80 as an example.
Listener Name
Custom
Advanced Configuration
Use the default configurations.
In the Select Server Group wizard, from the Select Server Group drop-down list, select Function Compute. Select the server group that you created in Step 2: Create a server group of the Function Compute type and then click Next.
In the Review Configurations wizard, review the configurations and click Submit.
Step 4: Test the connectivity
After you complete the preceding steps, a connection is established between Function Compute and ALB. You can perform the following operations to test the connectivity between Function Compute and ALB.
Open a command-line window and run the curl -v command to obtain the Date header field.
Function Compute requires that the HTTP request header includes the Date field.
curl -v <ALB instance domain name>
Run the curl -H command to access the domain name of the ALB instance.
curl -H "Date header field" <ALB instance domain name>
If a response similar to the one in the following figure is returned, this indicates that ALB can forward requests to Function Compute to invoke the function.
More information
- 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.