Use Application Load Balancer (ALB) as an event source for Function Compute by specifying a function as a backend server of an ALB instance. ALB forwards incoming HTTP requests to your function and returns the function's response to the requester. All traffic between ALB and Function Compute travels over Alibaba Cloud's secure internal network.
Limitations
Before you set up an ALB trigger, check the following constraints:
ALB triggers are only available in certain regions. For the full list, see Regions in which you can add Function Compute as a backend service of ALB.
The ALB instance and the function must be in the same region.
Each server group can have only one Function Compute backend.
If you created the function in Function Compute 2.0 with Handler Type set to Event Handler, configure an HTTP trigger on the function before adding it as a backend server.
Key features
Serverless architecture: Adding Function Compute as a backend service of ALB lets you build serverless applications with ease, significantly reducing O&M costs.
Auto scaling: Function Compute automatically scales computing resources up or down based on traffic, ensuring sufficient computing power during peak hours while conserving resources during off-peak hours.
High availability and fault tolerance: ALB and Function Compute work together to provide high availability and disaster recovery capabilities, ensuring stable application operation.
Use cases
Microservices routing: Use ALB's routing rules to distribute requests across multiple function-based microservices. Function Compute scales each function independently to handle concurrent load.
Real-time data processing: Route data processing requests to specialized functions, keeping business logic minimal and configurations simple.
Event-driven backends: Forward events from ALB to functions that process them and write results to a database or downstream service.
Image and video processing: Send uploaded media to processing functions. Function Compute automatically scales compute resources to match the workload.
Prerequisites
Before you begin, ensure that you have:
An ALB instance. See Create an ALB instance.
Set up an ALB trigger
Step 1: Create a function
Log on to the Function Compute console. In the left-side navigation pane, choose Function Management > Functions.
Select a region in the top navigation bar, then click Create Function.
On the Create Function page, choose Event Function, select a runtime and a default sample code, and click Create. This example uses the built-in Node.js runtime.
On the Code tab of the Function Details page, click Test Function to verify the function runs. A successful test returns
hello world.
Step 2: Create a server group of the Function Compute type
Log on to the Server Load Balancer (SLB) console. In the left-side navigation pane, choose ALB > Server Groups.
Select a region in the top navigation bar, then click Create Server Group.
In the Create Server Group dialog box, set Server Group Type to Function Compute, enter a name for the server group, and click Create.
ImportantIf you enable health checks, each health check probe counts as a Function Compute invocation and incurs charges. For billing details, see Resource usage items.

In the confirmation dialog, click Add Backend Server.
In the Add Backend Server panel, select the function you created and click OK. This example sets Configuration Method to Service, Function to the created function, and Version to LATEST. To use the Alibaba Cloud Resource Name (ARN) configuration method instead, first retrieve the function ARN. See Obtain the ARN of a function.

Step 3: Configure a listener
In the left-side navigation pane, choose ALB > Instances. Find the ALB instance you want to configure and click its ID.
Click the Listener tab, then click Create Listener.
In the Configure Listener step, set the protocol and port, then click Next. Use HTTP on port 80 for this example. For configuration options, see Add an HTTP listener.

In the Select Server Group step, select Function Compute from the Server Group drop-down list, select the server group you created in Step 2, and click Next.

In the Configuration Review step, confirm the configuration and click Submit.
Step 4: Test the connectivity
Run the following command to verify that ALB forwards requests to your function:
curl alb-n9p0q18eh2pbw****.{region_id}.alb.aliyuncsslb.comReplace alb-n9p0q18eh2pbw****.{region_id}.alb.aliyuncsslb.com with the actual domain name of your ALB instance.

A successful response confirms that ALB is forwarding requests to Function Compute.
For intranet access, the Virtual Private Cloud (VPC) containing your on-premises network must be the same VPC where the ALB instance is deployed.
If you have configured DNS resolution and bound a custom domain name, replace the ALB domain with your custom domain. See Step 4: Configure domain name resolution.
What's next
Use a custom domain name in production: Set up a CNAME record to resolve your custom domain to the ALB instance's domain name. Before binding a custom domain, register the domain and complete the ICP filing process. For domain registration, see Register a domain name on Alibaba Cloud. For ICP filing, see ICP filing process.
Review health check billing: If you enabled health checks on the server group, review the billing impact. For billing details, see Billable items and unit prices. For server group creation options, see Create a server group.