Tencentserverless SDK Overview
Tencentserverless is a Tencent Cloud SCF SDK that integrates SCF business flow APIs to make it easier to invoke SCF functions. It allows users to invoke a function quickly from a local system, CVM instance, container or function, eliminating the need to encapsulate APIs in a public cloud.
Features
Tencentserverless SDK has the following features:
It can invoke functions in a high-performance, low-latency manner.
Enables quick invocation across functions after the required parameters are entered (it will obtain parameters in environment variables by default, such as
region and SecretId).Supports access with private network domain names.
Supports session keep-alive.
It supports cross-region function invocation.
Supports native invocation methods in Python.
Note
Calling SDK across functions is only applicable to event-triggered functions. HTTP-triggered functions can be invoked by requesting the corresponding path of the function in the function code.
Quick Start
Mutual function invocation
Sample
Note
To implement mutual recursion of functions in different regions, you need to specify the region. For the naming rules, please see Region List.
If no region is specified, intra-region mutual function invocation will be used by default.
If no namespace is specified,
default will be used by default.1. Create a Python cloud function on the cloud, named "FuncInvoked" in the Guangzhou region. The function content is as follows:
# -*- coding: utf8 -*-def main_handler(event, context):if 'key1' in event.keys():print("value1 = " + event['key1'])if 'key2' in event.keys():print("value2 = " + event['key2'])return "Hello World from the function being invoked" #return
2. Create a Python cloud function on the cloud, named "PythonInvokeTest" in the Chengdu region. You can edit the PythonInvokeTest function in one of the following two ways, depending on your actual situation.
Method 1. If you don't need to invoke the function frequently, you can use the following sample code:
from tencentserverless import scffrom tencentserverless.scf import Clientfrom tencentserverless.exception import TencentServerlessSDKExceptionfrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptiondef main_handler(event, context):print("prepare to invoke a function!")try:data = scf.invoke('FuncInvoked',region="ap-guangzhou",data={"a": "b"})print (data)except TencentServerlessSDKException as e:print (e)except TencentCloudSDKException as e:print (e)except Exception as e:print (e)return "Already invoked a function!" # return
The output is as follows:
"Already invoked a function!"
Method 2. If you need to invoke the function frequently, you can choose to connect and trigger it through
Client by using the following sample code:# -*- coding: utf8 -*-from tencentserverless import scffrom tencentserverless.scf import Clientfrom tencentserverless.exception import TencentServerlessSDKExceptionfrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptiondef main_handler(event, context):#scf = Client(region="ap-guangzhou") # To connect using this method, please enable the "Execution Role" feature in the function configuration, and select an execution role with function invocation permissions.scf = Client(secret_id="AKIxxxxxxxxxxxxxxxxxxxxxxggB4Sa", secret_key="3vZzxxxxxxxxxxxaeTC", region="ap-guangzhou", token="") # To connect using this method, please replace the secret_id and secret_key with your own. This pair of keys needs to include the permission to invoke the function.print("prepare to invoke a function!")try:data = scf.invoke('FuncInvoked',data={"a":"b"})data = scf.FuncInvoked(data={"a": "b"}) # To use the native Python invocation method, you need to initialize it throughClientfirst.print (data)except TencentServerlessSDKException as e:print (e)except TencentCloudSDKException as e:print (e)except Exception as e:print (e)return "Already invoked a function!" # return
The output is as follows:
"Already invoked a function!"
Note
Secret_id and secret_key refer to the ID and Key of the Cloud API. You can obtain or create the relevant keys by logging into the Access Management Console, selecting Cloud API Keys, and then API Key Management.
Local function invocation
Development Preparations
Development Environment:
Python 2.7 or Python 3.6 should be installed.
Runtime Environment:
The tencentserverless SDK should be installed, and it supports Windows, Linux, and Mac operating systems.
Note
For local function invocation, you must complete the above preparations. We recommend you develop the function locally and then upload it to the cloud and use mutual function invocation for debugging.
Installation through pip (recommended)
Run the following command to install Tencentserverless SDK for Python.
pip install tencentserverless
Installing through source package
Download the latest source code package from the Github repository. After decompressing the source code package, run the following commands in sequence to install.
cd tencent-serverless-python-masterpython setup.py install
Configuring Tencentserverless SDK for Python
Run the following command to upgrade Tencentserverless SDK for Python.
pip install tencentserverless -U
Run the following command to view the information of Tencentserverless SDK for Python.
pip show tencentserverless
Sample
1. Create a Python cloud function on the cloud, named "FuncInvoked" in the Guangzhou region. The function content is as follows:
# -*- coding: utf8 -*-def main_handler(event, context):if 'key1' in event.keys():print("value1 = " + event['key1'])if 'key2' in event.keys():print("value2 = " + event['key2'])return "Hello World from the function being invoked" #return
2. Create a local file named
PythonInvokeTest.py with the following content:# -*- coding: utf8 -*-from tencentserverless import scffrom tencentserverless.scf import Clientfrom tencentserverless.exception import TencentServerlessSDKExceptionfrom tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKExceptiondef main_handler(event, context):print("prepare to invoke a function!")scf = Client(secret_id="AKIxxxxxxxxxxxxxxxxxxxxxxggB4Sa", secret_key="3vZzxxxxxxxxxxxaeTC", region="ap-guangzhou", token="") # Replace with your own secret_id and secret_keytry:data = scf.invoke('FuncInvoked',data={"a":"b"})# data = scf.FuncInvoked(data={"a":"b"})print (data)except TencentServerlessSDKException as e:print (e)except TencentCloudSDKException as e:print (e)except Exception as e:print (e)return "Already invoked a function!" # returnmain_handler("","")
Go to the directory where the
PythonInvokeTest.py file is located and run the following command to view the result.python PythonInvokeTest.py
The output is as follows:
prepare to invoke a function!"Hello World form the function being invoked"
API List
API Reference
Client (class)
invoke (method)
TencentserverlessSDKException (class)
Client
Method:
__init__
Parameter information:
Parameter | Required | Local Disk Types | Description |
region | Not required | String | Region, which is the same as the region of the function invoking the API and is Guangzhou for local invocations by default. |
secret_id | Not required | String | User SecretId, which is obtained from the function's environment variable by default and is required for local debugging. |
secret_key | Not required | String | User SecretKey, which is obtained from the function's environment variable by default and is required for local debugging. |
token | Not required | String | User token, which is obtained from the function's environment variable by default. |
invoke
Parameter information:
Parameter | Required | Local Disk Types | Description |
function_name | Supported | String | Function name. |
qualifier | Not required | String | Function version. Default value: $LATEST. |
data | Not required | Object | Input parameter for function execution, which must be an object that can be processed by json.dumps. |
namespace | Not required | String | Namespace. Default value: default. |
invoke
This is used to invoke a function. Currently, only sync invocation is supported.
Parameter information:
Parameter | Required | Local Disk Types | Description |
region | Not required | String | Region, which is the same as the region of the function invoking the API and is Guangzhou for local invocations by default. |
secret_id | Not required | String | User SecretId, which is obtained from the function's environment variable by default and is required for local debugging. |
secret_key | Not required | String | User SecretKey, which is obtained from the function's environment variable by default and is required for local debugging. |
token | Not required | String | User token, which is obtained from the function's environment variable by default. |
function_name | Supported | String | Function name. |
qualifier | Not required | String | Function version. Default value: $LATEST. |
data | Not required | String | Input parameter for function execution, which must be an object that can be processed by json.dumps. |
namespace | Not required | String | Namespace. Default value: default. |
TencentserverlessSDKException
Attributes:
[code]
[message]
[request_id]
[response]
[stack_trace]
Methods and descriptions:
Method Name | Description |
get_code | Returns error code |
get_message | Returns error message |
get_request_id | Returns RequestId |
get_response | Returns response |
get_stack_trace | Returns stack_trace |