tencent cloud

Feedback

Binding with SCF

Last updated: 2024-10-10 17:14:12
    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

    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

    1. You have created a CLB instance.
    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

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support