You can implement backend web services by writing SCF functions and bind them with CLB instances to provide services.
Background
Tencent Cloud Serverless Cloud Function (SCF) is a serverless execution environment that enables you to build and run applications without having to purchase and manage servers. After creating a function, you can create a CLB trigger to bind the function and event. The CLB trigger will pass the request content as parameters to the function and return the result from the function back to the requester as the response. Overview
HTTP/HTTPS general access
Applicable to apps for ecommerce, social media and other services, and web applications for personal blogging, event pages and more. The workflow is as follows:
1. HTTP/HTTPS requests initiated by apps, browsers, H5 pages, or Mini Programs access the SCF function through the CLB instance.
2. After the CLB instance completes the certificate uninstallation, SCF only needs to provide HTTP services.
3. The request is then transferred to the SCF function for subsequent processing, such as writing to the cloud database and calling other APIs.
Switching between CVM and SCF
Applicable to migrating HTTP/HTTPS services from CVM to SCF, especially in the event of failover. The workflow is as follows:
1. The app, browser, H5, or Wechat Mini Program initiates an HTTP/HTTPS request.
2. The request is then resolved to two CLB instances’ VIPs by the DNS.
3. One CLB instance forwards the request to the CVM and the other forwards it to the SCF.
4. The switch from CVM to SCF on the backend is complete without affecting the client side.
CVM/SCF business diversion
Applicable to using SCF to handle highly elastic services and CVM to handle daily business in scenarios such as flash sales and snap-up purchase.
1. Through DNS resolution, domain name A is resolved to one CLB instance’s VIP and domain name B is resolved to the other CLB instance’s VIP.
2. One CLB instance forwards the request to the CVM and the other forwards it to the SCF.
Restrictions
Binding with SCF is only available in Guangzhou, Shanghai, Beijing, Chengdu, Hong Kong (China), Singapore, Mumbai, Tokyo, and Silicon Valley.
SCF functions can only be bound with CLB instances of bill-by-IP accounts but not with bill-by-CVM accounts. If you are using a bill-by-CVM account, we recommend upgrading it to a bill-by-IP account. For more information, please see Checking Account Type. SCF functions cannot be bound with classic CLB instances.
SCF functions cannot be bound with classic network-based CLB instances.
SCF functions in the same region can be bound with CLB instances. SCF functions can only be bound across VPCs but not regions.
SCF functions can only be bound with IPv4 and IPv6 NAT64 CLB instances, but currently not with IPv6 CLB instances.
SCF functions can only be bound with layer-7 HTTP and HTTPS listeners, but not with layer-7 QUIC listeners or layer-4 (TCP, UDP, and TCP SSL) listeners.
Only SCF event functions can be bound with CLB instances.
Prerequisites
2. You have configured an HTTP or an HTTPS listener. Directions
Step 1. Create a function
1. Log in to the SCF Console and click Function Service on the left sidebar. 2. On the Function Service page, click Create.
3. On the Create page, select Custom for the creation mode, and enter a function name. Then select the same region that you selected for your CLB instance and Python3.6 for the runtime environment, enter the following code in the input box (Hello CLB is used for illustration), and click Complete.
Note:
When you bind your CLB instance to the SCF function, content needs to be returned in the specific response integration format. For more informantion, see CLB Trigger. # -*- coding: utf8 -*-
import json
def main_handler(event, context):
return {
"isBase64Encoded": False,
"statusCode": 200,
"headers": {"Content-Type":"text/html"},
"body": "<html><body><h1>Hello CLB</h1></body></html>"
}
Step 2. Deploy the function
1. On the "Functions" list page, click the name of the function you created.
2. On the Function Management page, select the Function Codes tab and click Deploy at the bottom.
Step 3. Bind the function
1. Log in to the CLB Console and click Instance Management on the left sidebar. 2. On the Instance Management page, click Configure Listener on the right of an instance.
3. In the HTTP/HTTPS Listener section, select the listener to be bound with an SCF function. Click the + icon on the left of the listener and the domain name under it, select the URL path displayed, and click Bind.
4. In the pop-up window, select SCF as the target type, set the configuration items, and click Confirm.
5. On the Listener Management tab, you should see the function bound to the CLB instance in the Forwarding Rules section, indicating the CLB trigger is created.
Note:
You can also create a CLB trigger in the SCF console to bind the CLB instance with an SCF function. For more information, please see Creating Triggers. Result Validation
1. If you bind a public network CLB with a cloud function, and set the IP mode to Static IP, access the cloud function with the VIP and port of the CLB instance. Hello CLB indicates that the cloud function is deployed successfully.
2. If you bind a public network CLB with a cloud function, and set the IP mode to Dynamic IP, access the cloud function with the domain name and port of the CLB instance. Hello CLB indicates that the cloud function is deployed successfully.
3. If you bind a private network CLB with a cloud function, access the cloud function via a CVM in the same VPC as the CLB instance. Hello CLB indicates that the cloud function is deployed successfully.
References
Was this page helpful?